- Android (Java)zgshgys3w.com
MainActivity.java (Android Studio项目中的一部分)
java
package com.example.myapp;
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!");
}
}
2. iOS (Swift)
ViewController.swift (Xcode项目中的一部分)
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: 50, y: 50, width: 200, height: 20)
view.addSubview(label)
}
}
3. Flutter (Dart)
main.dart (Flutter项目中的主文件)
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!’),
),
),
);
}
}
4. React Native (JavaScript/TypeScript)
App.js (React Native项目中的主文件)
javascript
import React from ‘react’;
import { View, Text, StyleSheet } from ‘react-native’;
const App = () => {
return (
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
});
export default App;
请注意,这些示例仅展示了如何在一个基本的App中显示“Hello, World!”。要构建一个完整的、功能丰富的App,你还需要学习更多的内容,如布局、导航、状态管理、网络请求、数据存储等。由于生成一个完整的App代码涉及多个方面(如UI设计、后端逻辑、数据库等),并且不同平台(如iOS、Android、Web)有不同的编程语言和工具,这里我将为你提供几个简单的示例,每个示例都将使用不同的编程语言来展示如何开始一个基本的App项目。
- Android (Java)
MainActivity.java (Android Studio项目中的一部分)
java
package com.example.myapp;
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!");
}
}
2. iOS (Swift)
ViewController.swift (Xcode项目中的一部分)
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: 50, y: 50, width: 200, height: 20)
view.addSubview(label)
}
}
3. Flutter (Dart)
main.dart (Flutter项目中的主文件)
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!’),
),
),
);
}
}
4. React Native (JavaScript/TypeScript)
App.js (React Native项目中的主文件)
javascript
import React from ‘react’;
import { View, Text, StyleSheet } from ‘react-native’;
const App = () => {
return (
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
});
export default App;
请注意,这些示例仅展示了如何在一个基本的App中显示“Hello, World!”。要构建一个完整的、功能丰富的App,你还需要学习更多的内容,如布局、导航、状态管理、网络请求、数据存储等。