- Java (Android 应用)daruijixie.cn
Java 通常用于编写 Android 应用的后端逻辑。
java
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) {
- Toast.makeText(MainActivity.this, "Button clicked!", Toast.LENGTH_SHORT).show();
- }
- });
- }
复制代码 }
2. Kotlin (Android 应用)
Kotlin 是 Android 开发的官方推荐语言。
kotlin
class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val button = findViewById<Button>(R.id.my_button)
- button.setOnClickListener {
- Toast.makeText(this, "Button clicked!", Toast.LENGTH_SHORT).show()
- }
- }
复制代码 }
3. Swift (iOS 应用)
Swift 用于编写 iOS 应用。
swift
import UIKit
class ViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- let button = UIButton(type: .system)
- button.setTitle("Click Me", for: .normal)
- button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
- button.translatesAutoresizingMaskIntoConstraints = false
- view.addSubview(button)
- NSLayoutConstraint.activate([
- button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
- button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
- ])
- }
- @objc func buttonClicked() {
- let alert = UIAlertController(title: "Alert", message: "Button clicked!", preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
- self.present(alert, animated: true, completion: nil)
- }
复制代码 }
4. Flutter (跨平台应用)
Flutter 使用 Dart 语言编写跨平台应用。
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, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
void _incrementCounter() {
// This is an example of a stateful widget update.
// In a real app, this might trigger a network fetch, or update a local database.
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this widget and that it needs to be redrawn.
});
}
@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(
‘0’, // Here you would normally use a variable to count the presses.
style: Theme.of(context).textTheme.headline4,
),
],
由于直接编写一个完整的应用程序的代码(如Android应用或iOS应用)涉及到多个文件、资源、布局以及可能的后端逻辑,这里我将为你提供几种编程语言中的简短示例,展示它们如何用于编写应用程序的差别部门。
- Java (Android 应用)
Java 通常用于编写 Android 应用的后端逻辑。
java
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) {
- Toast.makeText(MainActivity.this, "Button clicked!", Toast.LENGTH_SHORT).show();
- }
- });
- }
复制代码 }
2. Kotlin (Android 应用)
Kotlin 是 Android 开发的官方推荐语言。
kotlin
class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val button = findViewById<Button>(R.id.my_button)
- button.setOnClickListener {
- Toast.makeText(this, "Button clicked!", Toast.LENGTH_SHORT).show()
- }
- }
复制代码 }
3. Swift (iOS 应用)
Swift 用于编写 iOS 应用。
swift
import UIKit
class ViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- let button = UIButton(type: .system)
- button.setTitle("Click Me", for: .normal)
- button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
- button.translatesAutoresizingMaskIntoConstraints = false
- view.addSubview(button)
- NSLayoutConstraint.activate([
- button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
- button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
- ])
- }
- @objc func buttonClicked() {
- let alert = UIAlertController(title: "Alert", message: "Button clicked!", preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
- self.present(alert, animated: true, completion: nil)
- }
复制代码 }
4. Flutter (跨平台应用)
Flutter 使用 Dart 语言编写跨平台应用。
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, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
void _incrementCounter() {
// This is an example of a stateful widget update.
// In a real app, this might trigger a network fetch, or update a local database.
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this widget and that it needs to be redrawn.
});
}
@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(
‘0’, // Here you would normally use a variable to count the presses.
style: Theme.of(context).textTheme.headline4,
),
],
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |