帮我用androidStudio写一个计算器代码
时间: 2023-08-28 21:10:38 浏览: 109
好的,下面是一个简单的计算器代码,你可以在Android Studio中创建一个新项目并将其复制到MainActivity.java文件中:
```
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText resultEditText;
private Button[] numberButtons = new Button[10];
private Button[] operatorButtons = new Button[5];
private Button clearButton, equalButton;
private double num1 = 0, num2 = 0;
private String operator = "";
private boolean isOperatorPressed = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultEditText = findViewById(R.id.resultEditText);
clearButton = findViewById(R.id.clearButton);
equalButton = findViewById(R.id.equalButton);
for (int i = 0; i < numberButtons.length; i++) {
numberButtons[i] = findViewById(getResources().getIdentifier("button" + i, "id", getPackageName()));
numberButtons[i].setOnClickListener(this);
}
operatorButtons[0] = findViewById(R.id.additionButton);
operatorButtons[1] = findViewById(R.id.subtractionButton);
operatorButtons[2] = findViewById(R.id.multiplicationButton);
operatorButtons[3] = findViewById(R.id.divisionButton);
operatorButtons[4] = findViewById(R.id.decimalButton);
for (int i = 0; i < operatorButtons.length; i++) {
operatorButtons[i].setOnClickListener(this);
}
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resultEditText.setText("");
num1 = 0;
num2 = 0;
isOperatorPressed = false;
}
});
equalButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isOperatorPressed = false;
num2 = Double.parseDouble(resultEditText.getText().toString());
switch (operator) {
case "+":
resultEditText.setText(String.valueOf(num1 + num2));
break;
case "-":
resultEditText.setText(String.valueOf(num1 - num2));
break;
case "*":
resultEditText.setText(String.valueOf(num1 * num2));
break;
case "/":
if (num2 == 0) {
resultEditText.setText("Error");
} else {
resultEditText.setText(String.valueOf(num1 / num2));
}
break;
}
}
});
}
@Override
public void onClick(View v) {
if (isOperatorPressed) {
resultEditText.setText("");
}
isOperatorPressed = false;
switch (v.getId()) {
case R.id.button0:
resultEditText.append("0");
break;
case R.id.button1:
resultEditText.append("1");
break;
case R.id.button2:
resultEditText.append("2");
break;
case R.id.button3:
resultEditText.append("3");
break;
case R.id.button4:
resultEditText.append("4");
break;
case R.id.button5:
resultEditText.append("5");
break;
case R.id.button6:
resultEditText.append("6");
break;
case R.id.button7:
resultEditText.append("7");
break;
case R.id.button8:
resultEditText.append("8");
break;
case R.id.button9:
resultEditText.append("9");
break;
case R.id.additionButton:
operator = "+";
num1 = Double.parseDouble(resultEditText.getText().toString());
isOperatorPressed = true;
break;
case R.id.subtractionButton:
operator = "-";
num1 = Double.parseDouble(resultEditText.getText().toString());
isOperatorPressed = true;
break;
case R.id.multiplicationButton:
operator = "*";
num1 = Double.parseDouble(resultEditText.getText().toString());
isOperatorPressed = true;
break;
case R.id.divisionButton:
operator = "/";
num1 = Double.parseDouble(resultEditText.getText().toString());
isOperatorPressed = true;
break;
case R.id.decimalButton:
if (!resultEditText.getText().toString().contains(".")) {
resultEditText.append(".");
}
break;
}
}
}
```
此代码中,我们首先在onCreate方法中初始化UI元素,例如EditText和按钮。然后,我们为数字按钮和运算符按钮设置监听器,并设置相应的操作。最后,我们处理清除和等于按钮的点击事件,并执行相应的计算逻辑。
阅读全文