用android studio 写一个计算器代码
时间: 2023-10-05 15:07:36 浏览: 179
好的,以下是一个简单的计算器代码:
```
public class MainActivity extends AppCompatActivity {
private EditText etInput;
private TextView tvOutput;
private double num1 = 0, num2 = 0;
private String operator = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etInput = findViewById(R.id.et_input);
tvOutput = findViewById(R.id.tv_output);
Button btn0 = findViewById(R.id.btn_0);
Button btn1 = findViewById(R.id.btn_1);
Button btn2 = findViewById(R.id.btn_2);
Button btn3 = findViewById(R.id.btn_3);
Button btn4 = findViewById(R.id.btn_4);
Button btn5 = findViewById(R.id.btn_5);
Button btn6 = findViewById(R.id.btn_6);
Button btn7 = findViewById(R.id.btn_7);
Button btn8 = findViewById(R.id.btn_8);
Button btn9 = findViewById(R.id.btn_9);
Button btnAdd = findViewById(R.id.btn_add);
Button btnSubtract = findViewById(R.id.btn_subtract);
Button btnMultiply = findViewById(R.id.btn_multiply);
Button btnDivide = findViewById(R.id.btn_divide);
Button btnEquals = findViewById(R.id.btn_equals);
Button btnClear = findViewById(R.id.btn_clear);
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_0:
etInput.append("0");
break;
case R.id.btn_1:
etInput.append("1");
break;
case R.id.btn_2:
etInput.append("2");
break;
case R.id.btn_3:
etInput.append("3");
break;
case R.id.btn_4:
etInput.append("4");
break;
case R.id.btn_5:
etInput.append("5");
break;
case R.id.btn_6:
etInput.append("6");
break;
case R.id.btn_7:
etInput.append("7");
break;
case R.id.btn_8:
etInput.append("8");
break;
case R.id.btn_9:
etInput.append("9");
break;
case R.id.btn_add:
operator = "+";
num1 = Double.parseDouble(etInput.getText().toString());
etInput.setText("");
break;
case R.id.btn_subtract:
operator = "-";
num1 = Double.parseDouble(etInput.getText().toString());
etInput.setText("");
break;
case R.id.btn_multiply:
operator = "*";
num1 = Double.parseDouble(etInput.getText().toString());
etInput.setText("");
break;
case R.id.btn_divide:
operator = "/";
num1 = Double.parseDouble(etInput.getText().toString());
etInput.setText("");
break;
case R.id.btn_equals:
num2 = Double.parseDouble(etInput.getText().toString());
double result = 0;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
}
tvOutput.setText(String.valueOf(result));
break;
case R.id.btn_clear:
etInput.setText("");
tvOutput.setText("");
num1 = 0;
num2 = 0;
break;
}
}
};
btn0.setOnClickListener(onClickListener);
btn1.setOnClickListener(onClickListener);
btn2.setOnClickListener(onClickListener);
btn3.setOnClickListener(onClickListener);
btn4.setOnClickListener(onClickListener);
btn5.setOnClickListener(onClickListener);
btn6.setOnClickListener(onClickListener);
btn7.setOnClickListener(onClickListener);
btn8.setOnClickListener(onClickListener);
btn9.setOnClickListener(onClickListener);
btnAdd.setOnClickListener(onClickListener);
btnSubtract.setOnClickListener(onClickListener);
btnMultiply.setOnClickListener(onClickListener);
btnDivide.setOnClickListener(onClickListener);
btnEquals.setOnClickListener(onClickListener);
btnClear.setOnClickListener(onClickListener);
}
}
```
在 XML 文件中,你需要创建一个包含 EditText 和 TextView 的布局。这里是一个例子:
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter numbers here"
android:inputType="number"/>
<TextView
android:id="@+id/tv_output"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:gravity="center"/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="5"
android:padding="16dp">
<Button
android:id="@+id/btn_7"
android:layout_column="0"
android:layout_row="0"
android:text="7"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_8"
android:layout_column="1"
android:layout_row="0"
android:text="8"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_9"
android:layout_column="2"
android:layout_row="0"
android:text="9"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_divide"
android:layout_column="3"
android:layout_row="0"
android:text="/"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_4"
android:layout_column="0"
android:layout_row="1"
android:text="4"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_5"
android:layout_column="1"
android:layout_row="1"
android:text="5"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_6"
android:layout_column="2"
android:layout_row="1"
android:text="6"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_multiply"
android:layout_column="3"
android:layout_row="1"
android:text="*"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_1"
android:layout_column="0"
android:layout_row="2"
android:text="1"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_2"
android:layout_column="1"
android:layout_row="2"
android:text="2"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_3"
android:layout_column="2"
android:layout_row="2"
android:text="3"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_subtract"
android:layout_column="3"
android:layout_row="2"
android:text="-"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_0"
android:layout_column="0"
android:layout_row="3"
android:layout_columnSpan="2"
android:text="0"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_clear"
android:layout_column="2"
android:layout_row="3"
android:text="C"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_add"
android:layout_column="3"
android:layout_row="3"
android:text="+"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_equals"
android:layout_column="0"
android:layout_row="4"
android:layout_columnSpan="4"
android:text="="
android:textSize="20sp"/>
</GridLayout>
</LinearLayout>
```
这个代码仅仅是一个简单的例子,你可以根据自己的需要进行修改和扩展。
阅读全文