安卓计算器包含加减乘除以及按钮0到9且能实现连乘连加的MainActivity代码
时间: 2023-06-15 07:05:00 浏览: 187
android 实现计算器加减乘除混合运算
以下是一个简单的实现加减乘除以及按钮0到9且能实现连乘连加的安卓计算器MainActivity代码:
```java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tvResult;
private Button btnAdd, btnSubtract, btnMultiply, btnDivide, btnClear, btnEquals, btnDecimal;
private Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
private String currentNumber = "";
private String currentOperator = "";
private double result = 0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = findViewById(R.id.tv_result);
btnAdd = findViewById(R.id.btn_add);
btnSubtract = findViewById(R.id.btn_subtract);
btnMultiply = findViewById(R.id.btn_multiply);
btnDivide = findViewById(R.id.btn_divide);
btnClear = findViewById(R.id.btn_clear);
btnEquals = findViewById(R.id.btn_equals);
btnDecimal = findViewById(R.id.btn_decimal);
btn0 = findViewById(R.id.btn_0);
btn1 = findViewById(R.id.btn_1);
btn2 = findViewById(R.id.btn_2);
btn3 = findViewById(R.id.btn_3);
btn4 = findViewById(R.id.btn_4);
btn5 = findViewById(R.id.btn_5);
btn6 = findViewById(R.id.btn_6);
btn7 = findViewById(R.id.btn_7);
btn8 = findViewById(R.id.btn_8);
btn9 = findViewById(R.id.btn_9);
btnAdd.setOnClickListener(this);
btnSubtract.setOnClickListener(this);
btnMultiply.setOnClickListener(this);
btnDivide.setOnClickListener(this);
btnClear.setOnClickListener(this);
btnEquals.setOnClickListener(this);
btnDecimal.setOnClickListener(this);
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);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_0:
currentNumber += "0";
tvResult.setText(currentNumber);
break;
case R.id.btn_1:
currentNumber += "1";
tvResult.setText(currentNumber);
break;
case R.id.btn_2:
currentNumber += "2";
tvResult.setText(currentNumber);
break;
case R.id.btn_3:
currentNumber += "3";
tvResult.setText(currentNumber);
break;
case R.id.btn_4:
currentNumber += "4";
tvResult.setText(currentNumber);
break;
case R.id.btn_5:
currentNumber += "5";
tvResult.setText(currentNumber);
break;
case R.id.btn_6:
currentNumber += "6";
tvResult.setText(currentNumber);
break;
case R.id.btn_7:
currentNumber += "7";
tvResult.setText(currentNumber);
break;
case R.id.btn_8:
currentNumber += "8";
tvResult.setText(currentNumber);
break;
case R.id.btn_9:
currentNumber += "9";
tvResult.setText(currentNumber);
break;
case R.id.btn_decimal:
if (!currentNumber.contains(".")) {
currentNumber += ".";
tvResult.setText(currentNumber);
}
break;
case R.id.btn_clear:
currentNumber = "";
currentOperator = "";
result = 0.0;
tvResult.setText("");
break;
case R.id.btn_add:
performOperation("+");
break;
case R.id.btn_subtract:
performOperation("-");
break;
case R.id.btn_multiply:
performOperation("*");
break;
case R.id.btn_divide:
performOperation("/");
break;
case R.id.btn_equals:
performEquals();
break;
}
}
private void performOperation(String operator) {
if (currentOperator.equals("")) {
result = Double.parseDouble(currentNumber);
currentNumber = "";
currentOperator = operator;
} else {
if (!currentNumber.equals("")) {
double current = Double.parseDouble(currentNumber);
switch (currentOperator) {
case "+":
result += current;
break;
case "-":
result -= current;
break;
case "*":
result *= current;
break;
case "/":
result /= current;
break;
}
currentNumber = "";
}
currentOperator = operator;
}
tvResult.setText(String.valueOf(result));
}
private void performEquals() {
if (!currentNumber.equals("") && !currentOperator.equals("")) {
double current = Double.parseDouble(currentNumber);
switch (currentOperator) {
case "+":
result += current;
break;
case "-":
result -= current;
break;
case "*":
result *= current;
break;
case "/":
result /= current;
break;
}
currentNumber = "";
currentOperator = "";
tvResult.setText(String.valueOf(result));
}
}
}
```
该代码使用了一个 `currentNumber` 变量来存储当前输入的数字,一个 `currentOperator` 变量来存储当前操作符,以及一个 `result` 变量来存储计算结果。当用户点击数字或操作符按钮时,通过调用相应的方法进行处理。其中,`performOperation` 方法用于处理加减乘除运算,`performEquals` 方法用于处理等于运算。最后,将结果显示在 `tvResult` TextView 中。
阅读全文