android studio 计算器实现 java
时间: 2023-06-28 13:06:04 浏览: 209
以下是一个简单的 Android Studio 计算器实现的 Java 代码:
MainActivity.java
```java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView display;
private Button buttonZero, buttonOne, buttonTwo, buttonThree, buttonFour, buttonFive, buttonSix, buttonSeven, buttonEight, buttonNine, buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonEqual, buttonClear;
private float num1 = 0, num2 = 0;
private boolean addition, subtraction, multiplication, division;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = findViewById(R.id.display);
buttonZero = findViewById(R.id.button_zero);
buttonOne = findViewById(R.id.button_one);
buttonTwo = findViewById(R.id.button_two);
buttonThree = findViewById(R.id.button_three);
buttonFour = findViewById(R.id.button_four);
buttonFive = findViewById(R.id.button_five);
buttonSix = findViewById(R.id.button_six);
buttonSeven = findViewById(R.id.button_seven);
buttonEight = findViewById(R.id.button_eight);
buttonNine = findViewById(R.id.button_nine);
buttonAdd = findViewById(R.id.button_add);
buttonSubtract = findViewById(R.id.button_subtract);
buttonMultiply = findViewById(R.id.button_multiply);
buttonDivide = findViewById(R.id.button_divide);
buttonEqual = findViewById(R.id.button_equal);
buttonClear = findViewById(R.id.button_clear);
buttonZero.setOnClickListener(this);
buttonOne.setOnClickListener(this);
buttonTwo.setOnClickListener(this);
buttonThree.setOnClickListener(this);
buttonFour.setOnClickListener(this);
buttonFive.setOnClickListener(this);
buttonSix.setOnClickListener(this);
buttonSeven.setOnClickListener(this);
buttonEight.setOnClickListener(this);
buttonNine.setOnClickListener(this);
buttonAdd.setOnClickListener(this);
buttonSubtract.setOnClickListener(this);
buttonMultiply.setOnClickListener(this);
buttonDivide.setOnClickListener(this);
buttonEqual.setOnClickListener(this);
buttonClear.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button_zero:
display.append("0");
break;
case R.id.button_one:
display.append("1");
break;
case R.id.button_two:
display.append("2");
break;
case R.id.button_three:
display.append("3");
break;
case R.id.button_four:
display.append("4");
break;
case R.id.button_five:
display.append("5");
break;
case R.id.button_six:
display.append("6");
break;
case R.id.button_seven:
display.append("7");
break;
case R.id.button_eight:
display.append("8");
break;
case R.id.button_nine:
display.append("9");
break;
case R.id.button_add:
if (display.getText().length() != 0) {
num1 = Float.parseFloat(display.getText().toString());
addition = true;
display.setText("");
}
break;
case R.id.button_subtract:
if (display.getText().length() != 0) {
num1 = Float.parseFloat(display.getText().toString());
subtraction = true;
display.setText("");
}
break;
case R.id.button_multiply:
if (display.getText().length() != 0) {
num1 = Float.parseFloat(display.getText().toString());
multiplication = true;
display.setText("");
}
break;
case R.id.button_divide:
if (display.getText().length() != 0) {
num1 = Float.parseFloat(display.getText().toString());
division = true;
display.setText("");
}
break;
case R.id.button_equal:
if (display.getText().length() != 0) {
num2 = Float.parseFloat(display.getText().toString());
if (addition) {
display.setText(String.valueOf(num1 + num2));
addition = false;
} else if (subtraction) {
display.setText(String.valueOf(num1 - num2));
subtraction = false;
} else if (multiplication) {
display.setText(String.valueOf(num1 * num2));
multiplication = false;
} else if (division) {
display.setText(String.valueOf(num1 / num2));
division = false;
}
}
break;
case R.id.button_clear:
display.setText("");
num1 = 0;
num2 = 0;
addition = false;
subtraction = false;
multiplication = false;
division = false;
break;
}
}
}
```
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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:background="@drawable/edit_text_background"
android:gravity="end"
android:padding="16dp"
android:textSize="24sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/button_seven"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"/>
<Button
android:id="@+id/button_eight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"/>
<Button
android:id="@+id/button_nine"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"/>
<Button
android:id="@+id/button_divide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/button_four"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"/>
<Button
android:id="@+id/button_five"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"/>
<Button
android:id="@+id/button_six"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"/>
<Button
android:id="@+id/button_multiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/button_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"/>
<Button
android:id="@+id/button_two"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"/>
<Button
android:id="@+id/button_three"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"/>
<Button
android:id="@+id/button_subtract"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/button_zero"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"/>
<Button
android:id="@+id/button_clear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C"/>
<Button
android:id="@+id/button_equal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="="/>
<Button
android:id="@+id/button_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"/>
</LinearLayout>
</LinearLayout>
```
这个计算器实现了加、减、乘、除四种基本运算,以及清空和等于两个功能。用户可以通过点击数字按钮输入数字,通过点击运算按钮进行相应的运算,最终结果会显示在文本框中。
阅读全文