安卓系统app开发demo

安卓系统APP开发Demo:创建一个简单的计算器应用

在这篇教程中,我们将开发一个简单的计算器应用。此应用可以执行加、减、乘、除基本运算。我们将通过以下步骤来完成:

1. 安装Android Studio(安卓开发工具)

2. 创建新项目

3. 设计应用界面

4. 编写代码实现功能

5. 运行和测试应用

### 1. 安装Android Studio

首先,您需要安装Android Studio。您可以从官网(https://developer.android.com/studio)下载对应操作系统的安装包。安装完成后,打开Android Studio并配置SDK等相关设置。

### 2. 创建新项目

启动Android Studio,点击【开始新项目】。填写以下内容:

- 项目名称:CalculatorDemo

- 项目位置:选择适合的文件夹

- 语言:选择Java或Kotlin(我们以Java为例)

- 最小支持的API版本:选择Android 5.0以上

- 然后点击【完成】开始创建项目

### 3. 设计应用界面

在Android Studio的布局编辑器中,找到【activity_main.xml】文件进行编辑。这是应用的主界面布局文件。

我们将使用线性布局、输入框、按钮等组件实现以下界面:

- 一个EditText用于输入数字

- 四个Button用于加、减、乘、除运算

- 一个TextView用于显示计算结果

代码如下:

```xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="16dp">

android:id="@+id/et_number"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:inputType="numberDecimal"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/btn_add"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="+" />

android:id="@+id/btn_subtract"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="-" />

android:id="@+id/btn_multiply"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="*" />

android:id="@+id/btn_divide"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="/" />

android:id="@+id/tv_result"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Result"

android:textSize="24sp" />

```

### 4. 编写代码实现功能

找到【MainActivity.java】文件,并在其中编写以下代码:

```java

public class MainActivity extends AppCompatActivity {

private EditText etNumber;

private Button btnAdd, btnSubtract, btnMultiply, btnDivide;

private TextView tvResult;

private double number1 = 0.0, number2 = 0.0, result = 0.0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

etNumber = findViewById(R.id.et_number);

btnAdd = findViewById(R.id.btn_add);

btnSubtract = findViewById(R.id.btn_subtract);

btnMultiply = findViewById(R.id.btn_multiply);

btnDivide = findViewById(R.id.btn_divide);

tvResult = findViewById(R.id.tv_result);

btnAdd.setOnClickListener(v -> {

calculation("+");

});

btnSubtract.setOnClickListener(v -> {

calculation("-");

});

btnMultiply.setOnClickListener(v -> {

calculation("*");

});

btnDivide.setOnClickListener(v -> {

calculation("/");

});

}

private void calculation(String operator) {

if (TextUtils.isEmpty(etNumber.getText())) {

etNumber.setError("Please enter a number");

return;

}

double newNumber = Double.parseDouble(etNumber.getText().toString());

if (operator.equals("+")) {

result = number1 + newNumber;

} else if (operator.equals("-")) {

result = number1 - newNumber;

} else if (operator.equals("*")) {

result = number1 * newNumber;

} else if (operator.equals("/")) {

if (newNumber != 0) {

result = number1 / newNumber;

} else {

etNumber.setError("Cannot divide by zero");

return;

}

}

number1 = result;

tvResult.setText(String.format("Result: %s", result));

etNumber.getText().clear();

}

}

```

### 5. 运行和测试应用

点击Android Studio的运行按钮,选择一个虚拟设备或连接的实体设备。应用将启动并显示在设备上。尝试输入数字和执行各种运算以验证我们的计算器是否按预期工作。

至此,我们已经完成了一个简单的计算器应用。您可以根据自己的需求,对界面和功能进行更多的定制和拓展。希望这篇教程对您有所帮助!

川公网安备 51019002001728号