Android 科学计算器代码
时间: 2023-11-03 16:59:20 浏览: 96
这是一个简单的 Android 科学计算器代码示例:
```java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText editText;
Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
Button buttonAdd, buttonSub, buttonMul, buttonDiv, buttonEqual, buttonClear;
float value1, value2;
boolean add, sub, mul, div;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
button0 = findViewById(R.id.button0);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button4 = findViewById(R.id.button4);
button5 = findViewById(R.id.button5);
button6 = findViewById(R.id.button6);
button7 = findViewById(R.id.button7);
button8 = findViewById(R.id.button8);
button9 = findViewById(R.id.button9);
buttonAdd = findViewById(R.id.buttonAdd);
buttonSub = findViewById(R.id.buttonSub);
buttonMul = findViewById(R.id.buttonMul);
buttonDiv = findViewById(R.id.buttonDiv);
buttonEqual = findViewById(R.id.buttonEqual);
buttonClear = findViewById(R.id.buttonClear);
button0.setOnClickListener(this);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
buttonAdd.setOnClickListener(this);
buttonSub.setOnClickListener(this);
buttonMul.setOnClickListener(this);
buttonDiv.setOnClickListener(this);
buttonEqual.setOnClickListener(this);
buttonClear.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button0:
editText.setText(editText.getText() + "0");
break;
case R.id.button1:
editText.setText(editText.getText() + "1");
break;
case R.id.button2:
editText.setText(editText.getText() + "2");
break;
case R.id.button3:
editText.setText(editText.getText() + "3");
break;
case R.id.button4:
editText.setText(editText.getText() + "4");
break;
case R.id.button5:
editText.setText(editText.getText() + "5");
break;
case R.id.button6:
editText.setText(editText.getText() + "6");
break;
case R.id.button7:
editText.setText(editText.getText() + "7");
break;
case R.id.button8:
editText.setText(editText.getText() + "8");
break;
case R.id.button9:
editText.setText(editText.getText() + "9");
break;
case R.id.buttonAdd:
if (editText == null) {
editText.setText("");
} else {
value1 = Float.parseFloat(editText.getText() + "");
add = true;
editText.setText(null);
}
break;
case R.id.buttonSub:
if (editText == null) {
editText.setText("");
} else {
value1 = Float.parseFloat(editText.getText() + "");
sub = true;
editText.setText(null);
}
break;
case R.id.buttonMul:
if (editText == null) {
editText.setText("");
} else {
value1 = Float.parseFloat(editText.getText() + "");
mul = true;
editText.setText(null);
}
break;
case R.id.buttonDiv:
if (editText == null) {
editText.setText("");
} else {
value1 = Float.parseFloat(editText.getText() + "");
div = true;
editText.setText(null);
}
break;
case R.id.buttonEqual:
value2 = Float.parseFloat(editText.getText() + "");
if (add) {
editText.setText(value1 + value2 + "");
add = false;
}
if (sub) {
editText.setText(value1 - value2 + "");
sub = false;
}
if (mul) {
editText.setText(value1 * value2 + "");
mul = false;
}
if (div) {
editText.setText(value1 / value2 + "");
div = false;
}
break;
case R.id.buttonClear:
editText.setText("");
break;
}
}
}
```
代码中使用了 `EditText` 控件来显示计算结果,并使用了多个 `Button` 控件作为计算器的按钮。计算器支持加、减、乘、除四则运算,以及清除和等号操作。
阅读全文