写一个Android版本的计算器页面
时间: 2023-08-14 21:06:04 浏览: 47
好的,以下是一个简单的Android计算器页面的示例代码:
XML布局文件:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et_display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:textSize="30sp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_7"
android:text="7"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_8"
android:text="8"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_9"
android:text="9"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_divide"
android:text="/"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_4"
android:text="4"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_5"
android:text="5"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_6"
android:text="6"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_multiply"
android:text="*"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_1"
android:text="1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_2"
android:text="2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_3"
android:text="3"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_minus"
android:text="-"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_0"
android:text="0"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_dot"
android:text="."
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_equal"
android:text="="
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_plus"
android:text="+"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
```
Java代码:
```java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {
private EditText mDisplay;
private String mOperand1 = "";
private String mOperand2 = "";
private String mOperator = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
mDisplay = findViewById(R.id.et_display);
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 btnDot = findViewById(R.id.btn_dot);
Button btnEqual = findViewById(R.id.btn_equal);
Button btnPlus = findViewById(R.id.btn_plus);
Button btnMinus = findViewById(R.id.btn_minus);
Button btnMultiply = findViewById(R.id.btn_multiply);
Button btnDivide = findViewById(R.id.btn_divide);
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);
btnDot.setOnClickListener(this);
btnEqual.setOnClickListener(this);
btnPlus.setOnClickListener(this);
btnMinus.setOnClickListener(this);
btnMultiply.setOnClickListener(this);
btnDivide.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_0:
mDisplay.append("0");
break;
case R.id.btn_1:
mDisplay.append("1");
break;
case R.id.btn_2:
mDisplay.append("2");
break;
case R.id.btn_3:
mDisplay.append("3");
break;
case R.id.btn_4:
mDisplay.append("4");
break;
case R.id.btn_5:
mDisplay.append("5");
break;
case R.id.btn_6:
mDisplay.append("6");
break;
case R.id.btn_7:
mDisplay.append("7");
break;
case R.id.btn_8:
mDisplay.append("8");
break;
case R.id.btn_9:
mDisplay.append("9");
break;
case R.id.btn_dot:
if (!mDisplay.getText().toString().contains(".")) {
mDisplay.append(".");
}
break;
case R.id.btn_plus:
mOperator = "+";
mOperand1 = mDisplay.getText().toString();
mDisplay.setText("");
break;
case R.id.btn_minus:
mOperator = "-";
mOperand1 = mDisplay.getText().toString();
mDisplay.setText("");
break;
case R.id.btn_multiply:
mOperator = "*";
mOperand1 = mDisplay.getText().toString();
mDisplay.setText("");
break;
case R.id.btn_divide:
mOperator = "/";
mOperand1 = mDisplay.getText().toString();
mDisplay.setText("");
break;
case R.id.btn_equal:
mOperand2 = mDisplay.getText().toString();
double result = calculateResult();
mDisplay.setText(String.valueOf(result));
break;
}
}
private double calculateResult() {
double operand1 = Double.parseDouble(mOperand1);
double operand2 = Double.parseDouble(mOperand2);
double result = 0;
switch (mOperator) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*":
result = operand1 * operand2;
break;
case "/":
result = operand1 / operand2;
break;
}
return result;
}
}
```
注意:这只是一个简单的示例,没有考虑很多情况,比如错误输入等。在实际开发中,需要更加完善的逻辑和错误处理。