在这里插入图片描述

  1. 前端(Web) - HTML/CSS/JavaScript
    HTML:

html

zboubao.cn My App

Welcome to My App

Click Me CSS (styles.css):

css
body {
font-family: Arial, sans-serif;
}

button {
padding: 10px 20px;
font-size: 16px;
}
JavaScript (app.js):

javascript
document.getElementById(‘myButton’).addEventListener(‘click’, function() {
alert(‘Button clicked!’);
});
2. 后端(Node.js)
javascript
const express = require(‘express’);
const app = express();
const port = 3000;

app.get(‘/’, (req, res) => {
res.send(‘Hello from My App Server!’);
});

app.listen(port, () => {
console.log(App listening at http://localhost:${port});
});
3. 移动开发(Android - Java)
(这里只是一个简单的Activity示例)

java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  

    findViewById(R.id.myButton).setOnClickListener(v -> {  
        Toast.makeText(this, "Button clicked!", Toast.LENGTH_SHORT).show();  
    });  
}  

}
(对应的XML布局文件activity_main.xml)

xml

<?xml version="1.0" encoding="utf-8"?>

<Button  
    android:id="@+id/myButton"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="Click Me"  
    android:layout_centerInParent="true"/>  
4. 移动开发(iOS - Swift) (Swift代码通常在Xcode的Interface Builder中配合UI元素一起使用)

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.center = view.center  
    view.addSubview(button)  
}  

@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)  
}  

}
这些只是非常基础的示例,用于展示如何使用不同的编程语言开始一个简单的APP项目。在真实的应用程序中,你需要考虑更多的因素,如数据持久化、网络通信、安全性、用户体验等。由于不同的编程语言通常用于不同的应用程序开发任务(如前端、后端、移动开发等),而且完整的APP代码涉及很多方面(UI、逻辑、数据库等),所以在这里我将为你提供几种不同编程语言中的简单示例代码,这些代码将展示如何开始一个基本的APP项目。

  1. 前端(Web) - HTML/CSS/JavaScript
    HTML:

html

My App

Welcome to My App

Click Me CSS (styles.css):

css
body {
font-family: Arial, sans-serif;
}

button {
padding: 10px 20px;
font-size: 16px;
}
JavaScript (app.js):

javascript
document.getElementById(‘myButton’).addEventListener(‘click’, function() {
alert(‘Button clicked!’);
});
2. 后端(Node.js)
javascript
const express = require(‘express’);
const app = express();
const port = 3000;

app.get(‘/’, (req, res) => {
res.send(‘Hello from My App Server!’);
});

app.listen(port, () => {
console.log(App listening at http://localhost:${port});
});
3. 移动开发(Android - Java)
(这里只是一个简单的Activity示例)

java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  

    findViewById(R.id.myButton).setOnClickListener(v -> {  
        Toast.makeText(this, "Button clicked!", Toast.LENGTH_SHORT).show();  
    });  
}  

}
(对应的XML布局文件activity_main.xml)

xml

<?xml version="1.0" encoding="utf-8"?>

<Button  
    android:id="@+id/myButton"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="Click Me"  
    android:layout_centerInParent="true"/>  
4. 移动开发(iOS - Swift) (Swift代码通常在Xcode的Interface Builder中配合UI元素一起使用)

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.center = view.center  
    view.addSubview(button)  
}  

@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)  
}  

}
这些只是非常基础的示例,用于展示如何使用不同的编程语言开始一个简单的APP项目。在真实的应用程序中,你需要考虑更多的因素,如数据持久化、网络通信、安全性、用户体验等。

Logo

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

更多推荐