- Swift (iOS)
swift
import UIKit
class ViewController: UIViewController { hongjet.cn
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let label = UILabel()
label.text = “Hello, World!”
label.frame = CGRect(x: 0, y: 0, width: 200, height: 50)
label.center = self.view.center
self.view.addSubview(label)
}
}
2. Java (Android)
java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- TextView textView = new TextView(this);
- textView.setText("Hello, World!");
- textView.setLayoutParams(new ViewGroup.LayoutParams(
- ViewGroup.LayoutParams.WRAP_CONTENT,
- ViewGroup.LayoutParams.WRAP_CONTENT));
- // Assuming you have a LinearLayout with id 'container' in your activity_main.xml
- // LinearLayout layout = (LinearLayout) findViewById(R.id.container);
- // layout.addView(textView);
- }
复制代码 }
留意:Android示例中通常使用XML布局文件,所以这里的TextView添加是注释掉的。
dart
import ‘package:flutter/material.dart’;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Flutter Demo’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: ‘Flutter Demo Home Page’),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Text(‘Hello, World!’),
),
);
}
}
以上代码只是每个平台App的出发点。要创建一个完整的App,你还必要添加更多的功能和逻辑。由于直接生成一个完整的App代码会非常复杂,并且会依赖于特定的开发框架宁静台(如iOS的Swift/Objective-C,Android的Java/Kotlin,大概跨平台的Flutter/React Native等),我会为你提供几种语言中创建简单App框架的示例。
- Swift (iOS)
swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let label = UILabel()
label.text = “Hello, World!”
label.frame = CGRect(x: 0, y: 0, width: 200, height: 50)
label.center = self.view.center
self.view.addSubview(label)
}
}
2. Java (Android)
java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- TextView textView = new TextView(this);
- textView.setText("Hello, World!");
- textView.setLayoutParams(new ViewGroup.LayoutParams(
- ViewGroup.LayoutParams.WRAP_CONTENT,
- ViewGroup.LayoutParams.WRAP_CONTENT));
- // Assuming you have a LinearLayout with id 'container' in your activity_main.xml
- // LinearLayout layout = (LinearLayout) findViewById(R.id.container);
- // layout.addView(textView);
- }
复制代码 }
留意:Android示例中通常使用XML布局文件,所以这里的TextView添加是注释掉的。
dart
import ‘package:flutter/material.dart’;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Flutter Demo’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: ‘Flutter Demo Home Page’),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Text(‘Hello, World!’),
),
);
}
}
以上代码只是每个平台App的出发点。要创建一个完整的App,你还必要添加更多的功能和逻辑。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |