在这里插入图片描述

  1. 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添加是注释掉的。

  1. Flutter (跨平台)
    main.dart

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框架的示例。

  1. 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添加是注释掉的。

  1. Flutter (跨平台)
    main.dart

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,你还需要添加更多的功能和逻辑。

Logo

欢迎加入西安开发者社区!我们致力于为西安地区的开发者提供学习、合作和成长的机会。参与我们的活动,与专家分享最新技术趋势,解决挑战,探索创新。加入我们,共同打造技术社区!

更多推荐