Android Studio 生成计算器
时间: 2023-11-02 13:08:37 浏览: 114
好的,让我们开始吧!
首先,在 Android Studio 中创建一个新的项目。
然后,在新项目的 `activity_main.xml` 文件中添加一个布局,用于显示计算器的界面。可以使用 LinearLayout、RelativeLayout 或者其他布局方式来实现。这里以 LinearLayout 为例:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 显示计算结果的文本框 -->
<TextView
android:id="@+id/resultTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:gravity="right"
android:text="@string/result_text" />
<!-- 数字和操作符按钮的布局 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- 数字按钮 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0" />
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6" />
<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7" />
<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="9" />
<Button
android:id="@+id/buttonDot"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="." />
<Button
android:id="@+id/buttonClear"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C" />
</LinearLayout>
</LinearLayout>
<!-- 操作符按钮 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/buttonAdd"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="+" />
<Button
android:id="@+id/buttonSubtract"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="-" />
<Button
android:id="@+id/buttonMultiply"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="×" />
<Button
android:id="@+id/buttonDivide"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="÷" />
<Button
android:id="@+id/buttonEquals"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="=" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
```
接下来,在 `MainActivity.java` 文件中,定义计算器的逻辑和事件处理。在 onCreate() 方法中,找到布局文件中的各个按钮,并设置它们的点击事件。
```java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView resultTextView;
private Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonDot, buttonClear, buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonEquals;
private String currentNumber = "";
private String currentOperator = "";
private String leftOperand = "";
private String rightOperand = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find all views by id
resultTextView = findViewById(R.id.resultTextView);
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);
buttonDot = findViewById(R.id.buttonDot);
buttonClear = findViewById(R.id.buttonClear);
buttonAdd = findViewById(R.id.buttonAdd);
buttonSubtract = findViewById(R.id.buttonSubtract);
buttonMultiply = findViewById(R.id.buttonMultiply);
buttonDivide = findViewById(R.id.buttonDivide);
buttonEquals = findViewById(R.id.buttonEquals);
// Set click listeners for all buttons
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);
buttonDot.setOnClickListener(this);
buttonClear.setOnClickListener(this);
buttonAdd.setOnClickListener(this);
buttonSubtract.setOnClickListener(this);
buttonMultiply.setOnClickListener(this);
buttonDivide.setOnClickListener(this);
buttonEquals.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button0:
numberClicked("0");
break;
case R.id.button1:
numberClicked("1");
break;
case R.id.button2:
numberClicked("2");
break;
case R.id.button3:
numberClicked("3");
break;
case R.id.button4:
numberClicked("4");
break;
case R.id.button5:
numberClicked("5");
break;
case R.id.button6:
numberClicked("6");
break;
case R.id.button7:
numberClicked("7");
break;
case R.id.button8:
numberClicked("8");
break;
case R.id.button9:
numberClicked("9");
break;
case R.id.buttonDot:
numberClicked(".");
break;
case R.id.buttonClear:
clearClicked();
break;
case R.id.buttonAdd:
operatorClicked("+");
break;
case R.id.buttonSubtract:
operatorClicked("-");
break;
case R.id.buttonMultiply:
operatorClicked("×");
break;
case R.id.buttonDivide:
operatorClicked("÷");
break;
case R.id.buttonEquals:
equalsClicked();
break;
default:
break;
}
}
private void numberClicked(String number) {
currentNumber += number;
resultTextView.setText(currentNumber);
}
private void clearClicked() {
currentNumber = "";
currentOperator = "";
leftOperand = "";
rightOperand = "";
resultTextView.setText(getString(R.string.result_text));
}
private void operatorClicked(String operator) {
if (currentNumber.isEmpty()) {
return;
}
if (currentOperator.isEmpty()) {
leftOperand = currentNumber;
currentNumber = "";
currentOperator = operator;
resultTextView.setText(leftOperand + " " + currentOperator);
} else {
rightOperand = currentNumber;
currentNumber = "";
calculateResult();
leftOperand = resultTextView.getText().toString();
currentOperator = operator;
resultTextView.setText(leftOperand + " " + currentOperator);
}
}
private void equalsClicked() {
if (currentNumber.isEmpty() || currentOperator.isEmpty()) {
return;
}
rightOperand = currentNumber;
calculateResult();
currentNumber = "";
currentOperator = "";
}
private void calculateResult() {
double result = 0.0;
double leftValue = Double.parseDouble(leftOperand);
double rightValue = Double.parseDouble(rightOperand);
switch (currentOperator) {
case "+":
result = leftValue + rightValue;
break;
case "-":
result = leftValue - rightValue;
break;
case "×":
result = leftValue * rightValue;
break;
case "÷":
result = leftValue / rightValue;
break;
default:
break;
}
resultTextView.setText(String.valueOf(result));
}
}
```
这里使用了四个字符串变量,分别用于保存当前输入的数字、当前的操作符、左操作数和右操作数。当用户点击数字按钮时,将数字添加到当前输入的数字中,并更新文本框的内容。当用户点击清除按钮时,将所有变量重置,并将文本框的内容设置为初始值。当用户点击操作符按钮时,根据当前的状态,将左操作数、当前操作符和当前输入的数字保存,并在文本框中显示左操作数和当前操作符。当用户点击等于号时,将右操作数设置为当前输入的数字,并计算出结果,并将结果显示在文本框中。
现在,运行应用程序,你就可以使用这个简单的计算器进行加减乘除运算了!
阅读全文