Android简易小项目源码
1. 项目简介
本项目是一个简易的Android小项目,用于展示如何创建一个基本的计算器应用程序。该应用程序可以执行基本的加、减、乘、除运算,并显示结果。
2. 项目结构
项目的目录结构如下:
- app
- src
- main
- java/com/example/calculator
- MainActivity.java
- res
- layout
- activity_main.xml
3. 项目代码
MainActivity.java
package com.example.calculator;
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;
public class MainActivity extends AppCompatActivity {
private EditText number1EditText;
private EditText number2EditText;
private TextView resultTextView;
private Button addButton;
private Button subtractButton;
private Button multiplyButton;
private Button divideButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1EditText = findViewById(R.id.number1EditText);
number2EditText = findViewById(R.id.number2EditText);
resultTextView = findViewById(R.id.resultTextView);
addButton = findViewById(R.id.addButton);
subtractButton = findViewById(R.id.subtractButton);
multiplyButton = findViewById(R.id.multiplyButton);
divideButton = findViewById(R.id.divideButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double number1 = Double.parseDouble(number1EditText.getText().toString());
double number2 = Double.parseDouble(number2EditText.getText().toString());
double result = number1 + number2;
resultTextView.setText(String.valueOf(result));
}
});
subtractButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double number1 = Double.parseDouble(number1EditText.getText().toString());
double number2 = Double.parseDouble(number2EditText.getText().toString());
double result = number1 - number2;
resultTextView.setText(String.valueOf(result));
}
});
multiplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double number1 = Double.parseDouble(number1EditText.getText().toString());
double number2 = Double.parseDouble(number2EditText.getText().toString());
double result = number1 * number2;
resultTextView.setText(String.valueOf(result));
}
});
divideButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double number1 = Double.parseDouble(number1EditText.getText().toString());
double number2 = Double.parseDouble(number2EditText.getText().toString());
double result = number1 / number2;
resultTextView.setText(String.valueOf(result));
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
xmlns:tools="
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/number1EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入第一个数字" />
<EditText
android:id="@+id/number2EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入第二个数字" />
<Button
android:id="@+id/addButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加" />
<Button
android:id="@+id/subtractButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="减" />
<Button
android:id="@+id/multiplyButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="乘" />
<Button
android:id="@+id/divideButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="除" />
<TextView
android:id="@+id/resultTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="16dp"
android:textSize="24sp" />
</LinearLayout>
4. 项目说明
该项目是一个简单的Android计算器应用程序。它包括一个主要的Activity,其中包含两个输入框(用于输入两个数字)、四个操作按钮(加、减、乘、除)和一个文本视图(用于显示结果)。
在onCreate
方法中,我们