- Swift (iOS App)gzjiazheng.cn
Swift是Apple的官方编程语言,用于开发iOS和macOS应用。
swift
import UIKit
class ViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- }
- @IBAction func buttonPressed(_ sender: Any) {
- // 响应按钮点击事件
- print("Button was pressed!")
- }
复制代码 }
2. Java (Android App)
Java是Android开发的重要语言之一。
java
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button button = findViewById(R.id.my_button);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 响应按钮点击事件
- System.out.println("Button was pressed!");
- }
- });
- }
复制代码 }
3. Flutter (跨平台App)
Flutter利用Dart语言进行开发。
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: 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 {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
‘You have pushed the button this many times:’,
),
Text(
‘$_counter’,
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: ‘Increment’,
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for Flutter code.
);
}
}
4. React Native (跨平台App)
React Native利用JavaScript和React进行开发。
JavaScript (React Native)
jsx
import React, { useState } from ‘react’;
import { View, Text, Button, StyleSheet } from ‘react-native’;
const App = () => {
const [count, setCount] = useState(0);
const onPress = () => {
setCount(count + 1);
};
return (
You have pressed the button {count} times
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
});
export default App;
这些示例只是App开发的一小部门由于一个完整的App代码涉及多个层面(如前端、后端、数据库等),并且不同的操作系统(如iOS、Android)和平台(如React Native、Flutter、Xamarin等)有不同的实现方式,因此在这里我将给出几种不同编程语言下创建App的简单示例或框架。
- Swift (iOS App)
Swift是Apple的官方编程语言,用于开发iOS和macOS应用。
swift
import UIKit
class ViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- }
- @IBAction func buttonPressed(_ sender: Any) {
- // 响应按钮点击事件
- print("Button was pressed!")
- }
复制代码 }
2. Java (Android App)
Java是Android开发的重要语言之一。
java
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button button = findViewById(R.id.my_button);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 响应按钮点击事件
- System.out.println("Button was pressed!");
- }
- });
- }
复制代码 }
3. Flutter (跨平台App)
Flutter利用Dart语言进行开发。
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: 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 {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
‘You have pushed the button this many times:’,
),
Text(
‘$_counter’,
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: ‘Increment’,
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for Flutter code.
);
}
}
4. React Native (跨平台App)
React Native利用JavaScript和React进行开发。
JavaScript (React Native)
jsx
import React, { useState } from ‘react’;
import { View, Text, Button, StyleSheet } from ‘react-native’;
const App = () => {
const [count, setCount] = useState(0);
const onPress = () => {
setCount(count + 1);
};
return (
You have pressed the button {count} times
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
});
export default App;
这些示例只是App开发的一小部门
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |