android的简单计算器代码
时间: 2024-01-07 09:06:08 浏览: 59
下面是一个使用 Java 编写的 Android 简单计算器代码示例:
```java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tvResult;
private String currentNumber;
private String nextNumber;
private int currentOperator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
currentNumber = "";
nextNumber = "";
currentOperator = -1;
tvResult = findViewById(R.id.tvResult);
Button btn0 = findViewById(R.id.btn0);
Button btn1 = findViewById(R.id.btn1);
Button btn2 = findViewById(R.id.btn2);
Button btn3 = findViewById(R.id.btn3);
Button btn4 = findViewById(R.id.btn4);
Button btn5 = findViewById(R.id.btn5);
Button btn6 = findViewById(R.id.btn6);
Button btn7 = findViewById(R.id.btn7);
Button btn8 = findViewById(R.id.btn8);
Button btn9 = findViewById(R.id.btn9);
Button btnClear = findViewById(R.id.btnClear);
Button btnPlus = findViewById(R.id.btnPlus);
Button btnMinus = findViewById(R.id.btnMinus);
Button btnMultiply = findViewById(R.id.btnMultiply);
Button btnDivide = findViewById(R.id.btnDivide);
Button btnEqual = findViewById(R.id.btnEqual);
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);
btnClear.setOnClickListener(this);
btnPlus.setOnClickListener(this);
btnMinus.setOnClickListener(this);
btnMultiply.setOnClickListener(this);
btnDivide.setOnClickListener(this);
btnEqual.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn0:
currentNumber += "0";
tvResult.setText(currentNumber);
break;
case R.id.btn1:
currentNumber += "1";
tvResult.setText(currentNumber);
break;
case R.id.btn2:
currentNumber += "2";
tvResult.setText(currentNumber);
break;
case R.id.btn3:
currentNumber += "3";
tvResult.setText(currentNumber);
break;
case R.id.btn4:
currentNumber += "4";
tvResult.setText(currentNumber);
break;
case R.id.btn5:
currentNumber += "5";
tvResult.setText(currentNumber);
break;
case R.id.btn6:
currentNumber += "6";
tvResult.setText(currentNumber);
break;
case R.id.btn7:
currentNumber += "7";
tvResult.setText(currentNumber);
break;
case R.id.btn8:
currentNumber += "8";
tvResult.setText(currentNumber);
break;
case R.id.btn9:
currentNumber += "9";
tvResult.setText(currentNumber);
break;
case R.id.btnClear:
currentNumber = "";
nextNumber = "";
currentOperator = -1;
tvResult.setText("0");
break;
case R.id.btnPlus:
if (currentNumber.length() > 0) {
nextNumber = currentNumber;
currentNumber = "";
currentOperator = 1;
tvResult.setText("+");
}
break;
case R.id.btnMinus:
if (currentNumber.length() > 0) {
nextNumber = currentNumber;
currentNumber = "";
currentOperator = 2;
tvResult.setText("-");
}
break;
case R.id.btnMultiply:
if (currentNumber.length() > 0) {
nextNumber = currentNumber;
currentNumber = "";
currentOperator = 3;
tvResult.setText("*");
}
break;
case R.id.btnDivide:
if (currentNumber.length() > 0) {
nextNumber = currentNumber;
currentNumber = "";
currentOperator = 4;
tvResult.setText("/");
}
break;
case R.id.btnEqual:
if (nextNumber.length() > 0 && currentNumber.length() > 0) {
float result = 0;
float num1 = Float.parseFloat(nextNumber);
float num2 = Float.parseFloat(currentNumber);
switch (currentOperator) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
result = num1 / num2;
break;
}
tvResult.setText(String.valueOf(result));
currentNumber = String.valueOf(result);
nextNumber = "";
currentOperator = -1;
}
break;
}
}
}
```
这个简单计算器支持加减乘除四则运算,使用了一个 TextView 控件来显示当前输入的数字和运算结果。它由多个 Button 控件组成,每个按钮绑定了一个点击事件,根据用户的输入,动态更新 TextView 中的文本。在用户点击等于号时,会将两个数字和运算符传递给一个算法,计算出结果并显示到 TextView 中。
阅读全文