目录
简单介绍
💡 功能说明
🧱 布局文件:activity_main.xml(语言类型:XML)
🧠 Java 主逻辑文件:MainActivity.java(语言类型:Java)
📱 程序运行效果
✅ 小结
简单介绍
本项目旨在通过一个简单的计算器案例,帮助大家更好地理解 Android 移动应用开发中布局、控件交互和事件处理的基础知识。通过 EditText
获取用户输入、Button
响应点击事件、TextView
显示结果,掌握输入输出和逻辑控制的完整流程。非常适合 Android Studio 初学者练习和入门。
💡 功能说明
用户输入两个整数和一个运算符(+、-、*、/),点击“计算”按钮后,程序会根据输入内容计算结果并显示。如果是除法且除数为 0,程序会给出错误提示。
🧱 布局文件:activity_main.xml
(语言类型:XML)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"android:orientation="vertical"><EditTextandroid:id="@+id/edt_1"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入一个数:" /><EditTextandroid:id="@+id/edt_2"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入一个数:" /><EditTextandroid:id="@+id/edt_3"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入一个符号(+ - * /):" /><Buttonandroid:id="@+id/btn_1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="计算" /><TextViewandroid:id="@+id/tv_result"android:layout_width="wrap_content"android:layout_height="wrap_content" />
</LinearLayout>
🧠 Java 主逻辑文件:MainActivity.java
(语言类型:Java)
package com.example.demo3;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {private EditText edt_1, edt_2, edt_3;private TextView tv_result;private Button btn_1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); // 绑定布局edt_1 = findViewById(R.id.edt_1);edt_2 = findViewById(R.id.edt_2);edt_3 = findViewById(R.id.edt_3);tv_result = findViewById(R.id.tv_result);btn_1 = findViewById(R.id.btn_1);btn_1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(MainActivity.this, "开始计算", Toast.LENGTH_SHORT).show();String inputA = edt_1.getText().toString();String inputB = edt_2.getText().toString();String operator = edt_3.getText().toString();try {int numA = Integer.parseInt(inputA);int numB = Integer.parseInt(inputB);switch (operator) {case "+":tv_result.setText(String.valueOf(numA + numB));break;case "-":tv_result.setText(String.valueOf(numA - numB));break;case "*":tv_result.setText(String.valueOf(numA * numB));break;case "/":if (numB != 0) {tv_result.setText(String.valueOf(numA / numB));} else {tv_result.setText("除数不能为0!");}break;default:tv_result.setText("无效的操作符");}} catch (NumberFormatException e) {tv_result.setText("请输入有效参数!");}}});}
}
📱 程序运行效果
运行程序后,界面会显示三个输入框,一个按钮,一个结果区域:
-
输入两个整数,例如:
6
和2
-
输入运算符,例如:
/
-
点击“计算”按钮
-
结果显示为:
3
如果输入的是除以0,例如 6 / 0
,则提示:“除数不能为0!”
✅ 小结
通过本案例,大家可以掌握基本的 Android 控件使用(EditText、Button、TextView),事件监听(setOnClickListener),以及简单的输入校验和逻辑处理。非常适合初学者练习 Android 项目开发。