- Swift (iOS App)daruijixie.cn
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: 21)
label.center = self.view.center
self.view.addSubview(label)
}
}
2. Java (Android App)
java
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView);
textView.setText("Hello, World!");
}
}
注意:这里假设你有一个名为activity_main.xml的布局文件,其中包含一个ID为textView的TextView。
- Python (Flask Web App)
python
from flask import Flask, render_template
app = Flask(name)
@app.route(‘/’)
def hello_world():
return render_template(‘index.html’, message=‘Hello, World!’)
if name == ‘main’:
app.run(debug=True)
注意:这里假设你有一个名为index.html的HTML模板文件,并使用Flask的Jinja2模板引擎。
- JavaScript (React Native App)
javascript
import React from ‘react’;
import { View, Text, StyleSheet } from ‘react-native’;
export default function App() {
return (
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
});
注意:React Native需要额外的配置和依赖项才能运行。
- Dart (Flutter App)
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!’),
),
);
}
}
注意:Flutter也需要额外的配置和依赖项才能运行。由于生成一个完整的App代码涉及多个层面(如前端、后端、数据库、UI/UX等),并且不同的App(如iOS、Android、Web等)使用不同的技术和工具,我将为你提供几种语言中创建简单App的框架或示例代码片段。
- Swift (iOS App)
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: 21)
label.center = self.view.center
self.view.addSubview(label)
}
}
2. Java (Android App)
java
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView);
textView.setText("Hello, World!");
}
}
注意:这里假设你有一个名为activity_main.xml的布局文件,其中包含一个ID为textView的TextView。
- Python (Flask Web App)
python
from flask import Flask, render_template
app = Flask(name)
@app.route(‘/’)
def hello_world():
return render_template(‘index.html’, message=‘Hello, World!’)
if name == ‘main’:
app.run(debug=True)
注意:这里假设你有一个名为index.html的HTML模板文件,并使用Flask的Jinja2模板引擎。
- JavaScript (React Native App)
javascript
import React from ‘react’;
import { View, Text, StyleSheet } from ‘react-native’;
export default function App() {
return (
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
});
注意:React Native需要额外的配置和依赖项才能运行。
- Dart (Flutter App)
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!’),
),
);
}
}
注意:Flutter也需要额外的配置和依赖项才能运行。