- Flutter (跨平台移动应用开发)
Flutter是一个由Google开发的开源移动应用开发工具包,用于构建美观、高性能的移动应用。它使用Dart语言。
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 {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
} gdtouhaozhoupu.cn
@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),
),
);
}
}
2. React Native (跨平台移动应用开发)
React Native使用JavaScript和React构建原生应用。
React Native示例 (JavaScript):
jsx
import React, { useState } from ‘react’;
import { View, Text, Button, StyleSheet } from ‘react-native’;
const App = () => {
const [count, setCount] = useState(0);
const incrementCount = () => {
setCount(count + 1);
};
return (
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
},
});
export default App;
3. Kotlin (Android原生应用开发)
Kotlin是Android官方推荐的开发语言,与Java完全兼容。
Kotlin示例 (Android Studio):
kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
private var counter = 0
private lateinit var textView: TextView
private lateinit var button: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById(R.id.textView)
button = findViewById(R.id.button)
button.setOnClickListener {
counter++
textView.text = "You have pushed the button this many times: $counter"
}
}
}
请注意,以上示例仅展示了如何构建一个简单的计数器应用,并未涉及网络请求、数据库交互等复杂功能。在实际开发中,你需要根据具体需求来扩展这些示例。同时,这些示例也省略了诸如布局文件、权限声明、依赖配置等必要的部分,这些在真实项目中是必需的。