用Android studio 做一个简易的计算器
时间: 2023-12-03 13:04:53 浏览: 35
好的,下面是一个简单的 Android Studio 计算器项目实现的步骤:
1. 打开 Android Studio,创建一个新的项目。
2. 在项目结构中,选择 "app" 文件夹,右键单击,选择 "New" -> "Activity" -> "Empty Activity"。
3. 在 "activity_main.xml" 文件中,添加一个布局文件,包含一个 EditText 用于显示计算结果,以及多个按钮用于输入数字和操作符。
4. 在 "MainActivity.java" 文件中,为每个按钮添加点击事件,处理用户输入的数字和操作符,并更新 EditText 中的计算结果。
5. 实现计算逻辑,处理用户输入的表达式,计算结果并将其显示在 EditText 中。可以使用 Java 中的数学函数库或自行编写计算逻辑。
6. 运行应用程序,测试计算器是否正常工作。
下面是一个简单的示例代码:
activity_main.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/result_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:textSize="30sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
android:textSize="20sp"/>
<Button
android:id="@+id/button_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"
android:textSize="20sp"/>
<Button
android:id="@+id/button_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"
android:textSize="20sp"/>
<Button
android:id="@+id/button_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"
android:textSize="20sp"/>
<Button
android:id="@+id/button_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"
android:textSize="20sp"/>
<Button
android:id="@+id/button_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"
android:textSize="20sp"/>
<Button
android:id="@+id/button_subtract"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"
android:textSize="20sp"/>
<Button
android:id="@+id/button_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"
android:textSize="20sp"/>
<Button
android:id="@+id/button_9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"
android:textSize="20sp"/>
<Button
android:id="@+id/button_multiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_clear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clear"
android:textSize="20sp"/>
<Button
android:id="@+id/button_0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"
android:textSize="20sp"/>
<Button
android:id="@+id/button_equals"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="="
android:textSize="20sp"/>
<Button
android:id="@+id/button_divide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>
```
MainActivity.java:
```java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText resultText;
Button button1, button2, button3, button4, button5, button6, button7, button8, button9, button0;
Button buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonEquals, buttonClear;
String currentNumber = "";
String currentOperator = "";
double result = 0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultText = findViewById(R.id.result_text);
button1 = findViewById(R.id.button_1);
button2 = findViewById(R.id.button_2);
button3 = findViewById(R.id.button_3);
button4 = findViewById(R.id.button_4);
button5 = findViewById(R.id.button_5);
button6 = findViewById(R.id.button_6);
button7 = findViewById(R.id.button_7);
button8 = findViewById(R.id.button_8);
button9 = findViewById(R.id.button_9);
button0 = findViewById(R.id.button_0);
buttonAdd = findViewById(R.id.button_add);
buttonSubtract = findViewById(R.id.button_subtract);
buttonMultiply = findViewById(R.id.button_multiply);
buttonDivide = findViewById(R.id.button_divide);
buttonEquals = findViewById(R.id.button_equals);
buttonClear = findViewById(R.id.button_clear);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
button0.setOnClickListener(this);
buttonAdd.setOnClickListener(this);
buttonSubtract.setOnClickListener(this);
buttonMultiply.setOnClickListener(this);
buttonDivide.setOnClickListener(this);
buttonEquals.setOnClickListener(this);
buttonClear.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_0:
currentNumber += "0";
resultText.setText(currentNumber);
break;
case R.id.button_1:
currentNumber += "1";
resultText.setText(currentNumber);
break;
case R.id.button_2:
currentNumber += "2";
resultText.setText(currentNumber);
break;
case R.id.button_3:
currentNumber += "3";
resultText.setText(currentNumber);
break;
case R.id.button_4:
currentNumber += "4";
resultText.setText(currentNumber);
break;
case R.id.button_5:
currentNumber += "5";
resultText.setText(currentNumber);
break;
case R.id.button_6:
currentNumber += "6";
resultText.setText(currentNumber);
break;
case R.id.button_7:
currentNumber += "7";
resultText.setText(currentNumber);
break;
case R.id.button_8:
currentNumber += "8";
resultText.setText(currentNumber);
break;
case R.id.button_9:
currentNumber += "9";
resultText.setText(currentNumber);
break;
case R.id.button_add:
currentOperator = "+";
result += Double.parseDouble(currentNumber);
currentNumber = "";
break;
case R.id.button_subtract:
currentOperator = "-";
result -= Double.parseDouble(currentNumber);
currentNumber = "";
break;
case R.id.button_multiply:
currentOperator = "*";
result *= Double.parseDouble(currentNumber);
currentNumber = "";
break;
case R.id.button_divide:
currentOperator = "/";
result /= Double.parseDouble(currentNumber);
currentNumber = "";
break;
case R.id.button_equals:
if (currentOperator.equals("+")) {
result += Double.parseDouble(currentNumber);
} else if (currentOperator.equals("-")) {
result -= Double.parseDouble(currentNumber);
} else if (currentOperator.equals("*")) {
result *= Double.parseDouble(currentNumber);
} else if (currentOperator.equals("/")) {
result /= Double.parseDouble(currentNumber);
}
currentNumber = "";
resultText.setText(String.valueOf(result));
break;
case R.id.button_clear:
currentNumber = "";
currentOperator = "";
result = 0.0;
resultText.setText("");
break;
default:
break;
}
}
}
```
这个计算器可以进行加、减、乘、除四种基本运算,支持连续输入多个数字和操作符,可以通过 Clear 按钮清除输入内容。