- Swift (用于iOS App开发)grsyzp.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 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 = findViewById(R.id.textView);
textView.setText("Hello, World!");
}
}
(注意:这需要一个与之关联的XML布局文件activity_main.xml)
- Python (使用Kivy框架创建跨平台App)
python
from kivy.app import App
from kivy.uix.label import Label
class TestApp(App):
def build(self):
return Label(text=‘Hello, World!’)
if name == ‘main’:
TestApp().run()
4. JavaScript (使用React Native创建跨平台App)
javascript
import React from ‘react’;
import { View, Text } from ‘react-native’;
const App = () => {
return (
);
};
export default App;
(注意:这只是一个组件,你需要一个完整的React Native项目结构来运行它)
- Flutter (Dart语言,用于创建跨平台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: Scaffold(
appBar: AppBar(
title: Text(‘Flutter Demo Home Page’),
),
body: Center(
child: Text(‘Hello, World!’),
),
),
);
}
}
这些示例展示了如何在不同的编程语言和框架中创建一个简单的“Hello, World!”应用。要创建一个完整的App,你需要考虑更多的因素,如用户界面设计、数据持久化、网络请求等。由于直接生成一个完整的App代码超出了简单回答的范围(特别是涉及多种语言和跨平台),我将为你提供一个简化的示例,展示如何在几种不同的编程语言中创建一个简单的“Hello, World!”应用。请注意,这些示例通常不会直接编译成一个完整的App,但它们可以作为你开始编写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 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 = findViewById(R.id.textView);
textView.setText("Hello, World!");
}
}
(注意:这需要一个与之关联的XML布局文件activity_main.xml)
- Python (使用Kivy框架创建跨平台App)
python
from kivy.app import App
from kivy.uix.label import Label
class TestApp(App):
def build(self):
return Label(text=‘Hello, World!’)
if name == ‘main’:
TestApp().run()
4. JavaScript (使用React Native创建跨平台App)
javascript
import React from ‘react’;
import { View, Text } from ‘react-native’;
const App = () => {
return (
);
};
export default App;
(注意:这只是一个组件,你需要一个完整的React Native项目结构来运行它)
- Flutter (Dart语言,用于创建跨平台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: Scaffold(
appBar: AppBar(
title: Text(‘Flutter Demo Home Page’),
),
body: Center(
child: Text(‘Hello, World!’),
),
),
);
}
}
这些示例展示了如何在不同的编程语言和框架中创建一个简单的“Hello, World!”应用。要创建一个完整的App,你需要考虑更多的因素,如用户界面设计、数据持久化、网络请求等。