- Flutter (Dart)meimiaomeimo.cn
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, 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),
),
);
}
}
2. React Native (JavaScript/TypeScript)
React Native 是一个用于构建原生应用的 JavaScript 框架。
javascript
import React from ‘react’;
import { View, Text, Button, StyleSheet } from ‘react-native’;
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
onPress = () => {
this.setState({ count: this.state.count + 1 });
};
render() {
return (
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
backgroundColor: ‘#F5FCFF’,
},
});
3. Java (Android)
Java 是 Android 开发的主要语言。
java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button button;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
button.setOnClickListener(v -> {
count++;
textView.setText("You pressed the button " + count + " times");
});
}
}
4. Swift (iOS)
Swift 是 iOS 开发的主要语言。
swift
import UIKit
class ViewController: UIViewController {
var count = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let button = UIButton(type: .system)
button.setTitle("Press Me", for: .normal)
button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
view.addSubview(button)
let textView = UITextView()
textView.isEditable = false
textView.text = "You pressed the button 0 times"
textView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(textView)
NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -100),
textView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
textView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.8)
])
}
@objc func buttonPressed() {
count += 1
textView.text = "You pressed the button \(count) times"
}
}
以上代码分别展示了如何在不同平台上使用不同的编程语言来创建一个简单的计数器应用。请注意,这些代码只是基础框架,你可能需要根据你的具体需求进行扩展和修改。由于不同的编程语言通常用于不同的应用程序开发环境和目标平台,这里我将为你提供几个常见编程语言的基本框架或示例代码,用于生成一个简单的移动应用或桌面应用的“骨架”。
- Flutter (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, 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),
),
);
}
}
2. React Native (JavaScript/TypeScript)
React Native 是一个用于构建原生应用的 JavaScript 框架。
javascript
import React from ‘react’;
import { View, Text, Button, StyleSheet } from ‘react-native’;
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
onPress = () => {
this.setState({ count: this.state.count + 1 });
};
render() {
return (
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
backgroundColor: ‘#F5FCFF’,
},
});
3. Java (Android)
Java 是 Android 开发的主要语言。
java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button button;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
button.setOnClickListener(v -> {
count++;
textView.setText("You pressed the button " + count + " times");
});
}
}
4. Swift (iOS)
Swift 是 iOS 开发的主要语言。
swift
import UIKit
class ViewController: UIViewController {
var count = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let button = UIButton(type: .system)
button.setTitle("Press Me", for: .normal)
button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
view.addSubview(button)
let textView = UITextView()
textView.isEditable = false
textView.text = "You pressed the button 0 times"
textView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(textView)
NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -100),
textView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
textView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.8)
])
}
@objc func buttonPressed() {
count += 1
textView.text = "You pressed the button \(count) times"
}
}
以上代码分别展示了如何在不同平台上使用不同的编程语言来创建一个简单的计数器应用。请注意,这些代码只是基础框架,你可能需要根据你的具体需求进行扩展和修改。