用最简洁的代码写出一个计算器android studio代码
时间: 2024-02-05 09:10:26 浏览: 21
以下是一个简单的计算器应用程序的 Android Studio 代码:
MainActivity.java
```
import android.support.v7.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 {
Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnAdd, btnSubtract, btnMultiply, btnDivide, btnEquals, btnDecimal, btnClear;
TextView display;
String currentNumber;
String operation;
double result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn0 = findViewById(R.id.btn0);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
btn4 = findViewById(R.id.btn4);
btn5 = findViewById(R.id.btn5);
btn6 = findViewById(R.id.btn6);
btn7 = findViewById(R.id.btn7);
btn8 = findViewById(R.id.btn8);
btn9 = findViewById(R.id.btn9);
btnAdd = findViewById(R.id.btnAdd);
btnSubtract = findViewById(R.id.btnSubtract);
btnMultiply = findViewById(R.id.btnMultiply);
btnDivide = findViewById(R.id.btnDivide);
btnEquals = findViewById(R.id.btnEquals);
btnDecimal = findViewById(R.id.btnDecimal);
btnClear = findViewById(R.id.btnClear);
display = findViewById(R.id.display);
btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btnAdd.setOnClickListener(this);
btnSubtract.setOnClickListener(this);
btnMultiply.setOnClickListener(this);
btnDivide.setOnClickListener(this);
btnEquals.setOnClickListener(this);
btnDecimal.setOnClickListener(this);
btnClear.setOnClickListener(this);
currentNumber = "";
operation = "";
result = 0;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn0:
currentNumber += "0";
display.setText(currentNumber);
break;
case R.id.btn1:
currentNumber += "1";
display.setText(currentNumber);
break;
case R.id.btn2:
currentNumber += "2";
display.setText(currentNumber);
break;
case R.id.btn3:
currentNumber += "3";
display.setText(currentNumber);
break;
case R.id.btn4:
currentNumber += "4";
display.setText(currentNumber);
break;
case R.id.btn5:
currentNumber += "5";
display.setText(currentNumber);
break;
case R.id.btn6:
currentNumber += "6";
display.setText(currentNumber);
break;
case R.id.btn7:
currentNumber += "7";
display.setText(currentNumber);
break;
case R.id.btn8:
currentNumber += "8";
display.setText(currentNumber);
break;
case R.id.btn9:
currentNumber += "9";
display.setText(currentNumber);
break;
case R.id.btnAdd:
calculate();
operation = "+";
break;
case R.id.btnSubtract:
calculate();
operation = "-";
break;
case R.id.btnMultiply:
calculate();
operation = "*";
break;
case R.id.btnDivide:
calculate();
operation = "/";
break;
case R.id.btnEquals:
calculate();
operation = "";
break;
case R.id.btnDecimal:
if (!currentNumber.contains(".")) {
currentNumber += ".";
}
display.setText(currentNumber);
break;
case R.id.btnClear:
currentNumber = "";
operation = "";
result = 0;
display.setText("");
break;
}
}
private void calculate() {
if (currentNumber.length() > 0) {
double number = Double.parseDouble(currentNumber);
currentNumber = "";
if (operation.equals("+")) {
result += number;
} else if (operation.equals("-")) {
result -= number;
} else if (operation.equals("*")) {
result *= number;
} else if (operation.equals("/")) {
result /= number;
} else {
result = number;
}
display.setText("" + result);
}
}
}
```
activity_main.xml
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<TextView
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="right"
android:padding="16dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/btnClear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/display"
android:layout_toLeftOf="@+id/btnDivide"
android:layout_weight="1"
android:text="C" />
<Button
android:id="@+id/btnDivide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/display"
android:layout_toLeftOf="@+id/btnMultiply"
android:layout_weight="1"
android:text="/" />
<Button
android:id="@+id/btnMultiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/display"
android:layout_toLeftOf="@+id/btnSubtract"
android:layout_weight="1"
android:text="*" />
<Button
android:id="@+id/btnSubtract"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/display"
android:layout_toLeftOf="@+id/btnAdd"
android:layout_weight="1"
android:text="-" />
<Button
android:id="@+id/btnAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/display"
android:layout_toLeftOf="@+id/btnEquals"
android:layout_weight="1"
android:text="+" />
<Button
android:id="@+id/btnEquals"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/display"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:text="=" />
<Button
android:id="@+id/btn7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/btnClear"
android:layout_toLeftOf="@+id/btn8"
android:layout_weight="1"
android:text="7" />
<Button
android:id="@+id/btn8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/btnClear"
android:layout_toLeftOf="@+id/btn9"
android:layout_weight="1"
android:text="8" />
<Button
android:id="@+id/btn9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/btnClear"
android:layout_toLeftOf="@+id/btnDivide"
android:layout_weight="1"
android:text="9" />
<Button
android:id="@+id/btn4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/btn7"
android:layout_toLeftOf="@+id/btn5"
android:layout_weight="1"
android:text="4" />
<Button
android:id="@+id/btn5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/btn8"
android:layout_toLeftOf="@+id/btn6"
android:layout_weight="1"
android:text="5" />
<Button
android:id="@+id/btn6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/btn9"
android:layout_toLeftOf="@+id/btnMultiply"
android:layout_weight="1"
android:text="6" />
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/btn4"
android:layout_toLeftOf="@+id/btn2"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/btn5"
android:layout_toLeftOf="@+id/btn3"
android:layout_weight="1"
android:text="2" />
<Button
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/btn6"
android:layout_toLeftOf="@+id/btnSubtract"
android:layout_weight="1"
android:text="3" />
<Button
android:id="@+id/btnDecimal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/btn0"
android:layout_toLeftOf="@+id/btnEquals"
android:layout_weight="1"
android:text="." />
<Button
android:id="@+id/btn0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/btnClear"
android:layout_toLeftOf="@+id/btnDecimal"
android:layout_weight="1"
android:text="0" />
</RelativeLayout>
```