用android studio实现一个计算器
时间: 2023-05-20 10:06:34 浏览: 161
Android Studio实现计算器
5星 · 资源好评率100%
好的,您可以使用 Android Studio 中的 Java 语言编写一个简单的计算器应用程序。以下是一个示例代码:
```
public class CalculatorActivity extends AppCompatActivity {
private EditText mEditText;
private Button mButton0, mButton1, mButton2, mButton3, mButton4, mButton5, mButton6, mButton7, mButton8, mButton9;
private Button mButtonAdd, mButtonSubtract, mButtonMultiply, mButtonDivide, mButtonEquals, mButtonClear;
private double mValueOne = Double.NaN;
private double mValueTwo;
private static final char ADDITION = '+';
private static final char SUBTRACTION = '-';
private static final char MULTIPLICATION = '*';
private static final char DIVISION = '/';
private char mAction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
mEditText = findViewById(R.id.editText);
mButton0 = findViewById(R.id.button0);
mButton1 = findViewById(R.id.button1);
mButton2 = findViewById(R.id.button2);
mButton3 = findViewById(R.id.button3);
mButton4 = findViewById(R.id.button4);
mButton5 = findViewById(R.id.button5);
mButton6 = findViewById(R.id.button6);
mButton7 = findViewById(R.id.button7);
mButton8 = findViewById(R.id.button8);
mButton9 = findViewById(R.id.button9);
mButtonAdd = findViewById(R.id.buttonAdd);
mButtonSubtract = findViewById(R.id.buttonSubtract);
mButtonMultiply = findViewById(R.id.buttonMultiply);
mButtonDivide = findViewById(R.id.buttonDivide);
mButtonEquals = findViewById(R.id.buttonEquals);
mButtonClear = findViewById(R.id.buttonClear);
mButton0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setText(mEditText.getText() + "0");
}
});
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setText(mEditText.getText() + "1");
}
});
mButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setText(mEditText.getText() + "2");
}
});
mButton3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setText(mEditText.getText() + "3");
}
});
mButton4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setText(mEditText.getText() + "4");
}
});
mButton5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setText(mEditText.getText() + "5");
}
});
mButton6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setText(mEditText.getText() + "6");
}
});
mButton7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setText(mEditText.getText() + "7");
}
});
mButton8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setText(mEditText.getText() + "8");
}
});
mButton9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setText(mEditText.getText() + "9");
}
});
mButtonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
mAction = ADDITION;
mEditText.setText(null);
}
});
mButtonSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
mAction = SUBTRACTION;
mEditText.setText(null);
}
});
mButtonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
mAction = MULTIPLICATION;
mEditText.setText(null);
}
});
mButtonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
mAction = DIVISION;
mEditText.setText(null);
}
});
mButtonEquals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
mEditText.setText(String.valueOf(mValueOne));
mValueOne = Double.NaN;
mAction = '0';
}
});
mButtonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setText("");
mValueOne = Double.NaN;
mValueTwo = Double.NaN;
}
});
}
private void computeCalculation() {
if (!Double.isNaN(mValueOne)) {
mValueTwo = Double.parseDouble(mEditText.getText().toString());
switch (mAction) {
case ADDITION:
mValueOne = mValueOne + mValueTwo;
break;
case SUBTRACTION:
mValueOne = mValueOne - mValueTwo;
break;
case MULTIPLICATION:
mValueOne = mValueOne * mValueTwo;
break;
case DIVISION:
mValueOne = mValueOne / mValueTwo;
break;
default:
break;
}
} else {
mValueOne = Double.parseDouble(mEditText.getText().toString());
}
}
}
```
这段代码实现了一个简单的计算器应用程序,包括数字按钮、加减乘除按钮、等于按钮和清除按钮。当用户按下数字按钮时,数字将显示在 EditText 中。当用户按下加减乘除按钮时,计算器将执行相应的操作。当用户按下等于按钮时,计算器将计算结果并显示在 EditText 中。当用户按下清除按钮时,EditText 将被清空。
阅读全文