android studio 连续加减乘除无优先级计算器代码
时间: 2023-09-13 13:09:28 浏览: 152
android studio计算器实现代码
以下是一个简单的基于 Android Studio 的连续加减乘除无优先级计算器代码:
1. 在布局文件中添加一个 TextView 用于显示计算结果,以及四个 Button 用于输入数字和操作符。
2. 在 MainActivity 类中定义四个变量用于存储计算器的状态:
```java
private String currentNumber = ""; // 当前输入的数字
private String lastOperator = ""; // 上一个操作符
private double result = 0; // 当前计算结果
private boolean isResultDisplayed = false; // 当前计算结果是否已经显示
```
3. 为四个 Button 添加 onClickListener,分别处理数字和操作符的输入。例如,输入数字时追加到 currentNumber 中,输入操作符时根据 lastOperator 计算上一个数字和当前数字的结果,并将结果存储到 result 中。
```java
// 处理数字的输入
public void onNumberClick(View view) {
String buttonText = ((Button) view).getText().toString();
if (isResultDisplayed) {
currentNumber = "";
isResultDisplayed = false;
}
currentNumber += buttonText;
updateResultDisplay(currentNumber);
}
// 处理操作符的输入
public void onOperatorClick(View view) {
String buttonText = ((Button) view).getText().toString();
if (lastOperator.equals("")) {
result = Double.parseDouble(currentNumber);
} else {
double current = Double.parseDouble(currentNumber);
switch (lastOperator) {
case "+":
result += current;
break;
case "-":
result -= current;
break;
case "*":
result *= current;
break;
case "/":
result /= current;
break;
}
}
currentNumber = "";
lastOperator = buttonText;
updateResultDisplay(String.valueOf(result));
}
```
4. 添加一个 updateResultDisplay 方法用于更新 TextView 中的计算结果。
```java
// 更新计算结果的显示
private void updateResultDisplay(String resultString) {
TextView resultTextView = findViewById(R.id.result_text_view);
resultTextView.setText(resultString);
}
```
完整代码如下:
MainActivity.java
```java
public class MainActivity extends AppCompatActivity {
private String currentNumber = ""; // 当前输入的数字
private String lastOperator = ""; // 上一个操作符
private double result = 0; // 当前计算结果
private boolean isResultDisplayed = false; // 当前计算结果是否已经显示
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// 处理数字的输入
public void onNumberClick(View view) {
String buttonText = ((Button) view).getText().toString();
if (isResultDisplayed) {
currentNumber = "";
isResultDisplayed = false;
}
currentNumber += buttonText;
updateResultDisplay(currentNumber);
}
// 处理操作符的输入
public void onOperatorClick(View view) {
String buttonText = ((Button) view).getText().toString();
if (lastOperator.equals("")) {
result = Double.parseDouble(currentNumber);
} else {
double current = Double.parseDouble(currentNumber);
switch (lastOperator) {
case "+":
result += current;
break;
case "-":
result -= current;
break;
case "*":
result *= current;
break;
case "/":
result /= current;
break;
}
}
currentNumber = "";
lastOperator = buttonText;
updateResultDisplay(String.valueOf(result));
}
// 处理等号的输入
public void onEqualClick(View view) {
if (!lastOperator.equals("")) {
double current = Double.parseDouble(currentNumber);
switch (lastOperator) {
case "+":
result += current;
break;
case "-":
result -= current;
break;
case "*":
result *= current;
break;
case "/":
result /= current;
break;
}
}
currentNumber = "";
lastOperator = "";
updateResultDisplay(String.valueOf(result));
isResultDisplayed = true;
}
// 处理清除的输入
public void onClearClick(View view) {
currentNumber = "";
lastOperator = "";
result = 0;
updateResultDisplay("");
isResultDisplayed = false;
}
// 更新计算结果的显示
private void updateResultDisplay(String resultString) {
TextView resultTextView = findViewById(R.id.result_text_view);
resultTextView.setText(resultString);
}
}
```
activity_main.xml
```xml
<LinearLayout
...
android:orientation="vertical">
<TextView
...
android:id="@+id/result_text_view"
android:textSize="32sp"
android:textStyle="bold"
android:gravity="right"
android:padding="16dp"
android:textColor="@android:color/black"
android:text=""/>
<LinearLayout
...
android:orientation="horizontal">
<Button
...
android:text="7"
android:onClick="onNumberClick"/>
<Button
...
android:text="8"
android:onClick="onNumberClick"/>
<Button
...
android:text="9"
android:onClick="onNumberClick"/>
<Button
...
android:text="/"
android:onClick="onOperatorClick"/>
</LinearLayout>
<LinearLayout
...
android:orientation="horizontal">
<Button
...
android:text="4"
android:onClick="onNumberClick"/>
<Button
...
android:text="5"
android:onClick="onNumberClick"/>
<Button
...
android:text="6"
android:onClick="onNumberClick"/>
<Button
...
android:text="*"
android:onClick="onOperatorClick"/>
</LinearLayout>
<LinearLayout
...
android:orientation="horizontal">
<Button
...
android:text="1"
android:onClick="onNumberClick"/>
<Button
...
android:text="2"
android:onClick="onNumberClick"/>
<Button
...
android:text="3"
android:onClick="onNumberClick"/>
<Button
...
android:text="-"
android:onClick="onOperatorClick"/>
</LinearLayout>
<LinearLayout
...
android:orientation="horizontal">
<Button
...
android:text="0"
android:onClick="onNumberClick"/>
<Button
...
android:text="."
android:onClick="onNumberClick"/>
<Button
...
android:text="C"
android:onClick="onClearClick"/>
<Button
...
android:text="+"
android:onClick="onOperatorClick"/>
</LinearLayout>
<Button
...
android:text="="
android:onClick="onEqualClick"/>
</LinearLayout>
```
阅读全文