在这里插入图片描述

  1. Swift (iOS App)yuhaosujiaofirst.com
    使用Swift创建一个简单的“Hello, World!” iOS App的ViewController.swift可能如下所示:

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: 100, width: 200, height: 50)  
    label.textColor = .black  
    self.view.addSubview(label)  
}  

}
2. Java (Android App)
使用Java创建一个简单的“Hello, World!” Android App的MainActivity.java可能如下所示:

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!");  
}  

}
(注意:这里假设你有一个名为activity_main.xml的布局文件,其中包含一个ID为textView的TextView。)

  1. Flutter (跨平台App)
    使用Flutter(Dart语言)创建一个简单的“Hello, World!” App的main.dart可能如下所示:

dart
import ‘package:flutter/material.dart’;

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Hello, World!’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text(‘Hello, World!’),
),
body: Center(
child: Text(‘Hello, World!’),
),
),
);
}
}
4. React Native (跨平台App)
使用React Native(JavaScript/TypeScript)创建一个简单的“Hello, World!” App的App.js可能如下所示:

javascript
import React from ‘react’;
import { View, Text, StyleSheet } from ‘react-native’;

const App = () => {
return (

Hello, World!

);
};

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
});

export default App;
这些示例只是开始,实际的App开发会涉及更多的细节和复杂性。由于生成一个完整的App代码涉及多个方面(如UI设计、后端逻辑、数据存储等),且不同平台的App(如iOS、Android、Web App等)使用不同的开发工具和语言,我将为你提供几个简单的示例,展示如何用几种流行的编程语言开始一个简单的App项目。

  1. Swift (iOS App)
    使用Swift创建一个简单的“Hello, World!” iOS App的ViewController.swift可能如下所示:

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: 100, width: 200, height: 50)  
    label.textColor = .black  
    self.view.addSubview(label)  
}  

}
2. Java (Android App)
使用Java创建一个简单的“Hello, World!” Android App的MainActivity.java可能如下所示:

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!");  
}  

}
(注意:这里假设你有一个名为activity_main.xml的布局文件,其中包含一个ID为textView的TextView。)

  1. Flutter (跨平台App)
    使用Flutter(Dart语言)创建一个简单的“Hello, World!” App的main.dart可能如下所示:

dart
import ‘package:flutter/material.dart’;

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Hello, World!’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text(‘Hello, World!’),
),
body: Center(
child: Text(‘Hello, World!’),
),
),
);
}
}
4. React Native (跨平台App)
使用React Native(JavaScript/TypeScript)创建一个简单的“Hello, World!” App的App.js可能如下所示:

javascript
import React from ‘react’;
import { View, Text, StyleSheet } from ‘react-native’;

const App = () => {
return (

Hello, World!

);
};

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
});

export default App;
这些示例只是开始,实际的App开发会涉及更多的细节和复杂性。

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐