在android studio中制作一个没有优先级,运行结果保留两位小数的多位数相加减的计算器
时间: 2024-02-18 20:03:43 浏览: 26
以下是一个简单的 Android Studio 计算器代码,支持多位数相加减运算,没有优先级,运行结果保留两位小数:
1. 创建一个新的 Android Studio 项目,并在 `activity_main.xml` 中添加一个输入框、一个文本框和四个按钮,分别用于输入数字、显示结果和执行加减运算。
```xml
<EditText
android:id="@+id/etInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入数字" />
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="结果:0.00"
android:textAlignment="center"
android:textSize="24sp" />
<Button
android:id="@+id/btnAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+" />
<Button
android:id="@+id/btnSubtract"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-" />
<Button
android:id="@+id/btnClear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="清空" />
<Button
android:id="@+id/btnCalculate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="计算" />
```
2. 在 `MainActivity.java` 中添加以下代码:
```java
public class MainActivity extends AppCompatActivity {
private EditText etInput;
private TextView tvResult;
private Button btnAdd, btnSubtract, btnClear, btnCalculate;
private double result = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etInput = findViewById(R.id.etInput);
tvResult = findViewById(R.id.tvResult);
btnAdd = findViewById(R.id.btnAdd);
btnSubtract = findViewById(R.id.btnSubtract);
btnClear = findViewById(R.id.btnClear);
btnCalculate = findViewById(R.id.btnCalculate);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
double num = Double.parseDouble(etInput.getText().toString());
result += num;
tvResult.setText(String.format("结果:%.2f", result));
etInput.setText("");
} catch (NumberFormatException e) {
Toast.makeText(MainActivity.this, "请输入有效数字", Toast.LENGTH_SHORT).show();
}
}
});
btnSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
double num = Double.parseDouble(etInput.getText().toString());
result -= num;
tvResult.setText(String.format("结果:%.2f", result));
etInput.setText("");
} catch (NumberFormatException e) {
Toast.makeText(MainActivity.this, "请输入有效数字", Toast.LENGTH_SHORT).show();
}
}
});
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result = 0;
tvResult.setText("结果:0.00");
etInput.setText("");
}
});
btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
double num = Double.parseDouble(etInput.getText().toString());
result += num;
tvResult.setText(String.format("结果:%.2f", result));
etInput.setText("");
} catch (NumberFormatException e) {
Toast.makeText(MainActivity.this, "请输入有效数字", Toast.LENGTH_SHORT).show();
}
}
});
}
}
```
代码思路:
1. 在 `onCreate` 方法中,使用 `findViewById` 方法来获取输入框、文本框和四个按钮的实例。
2. 为加、减、清空和计算按钮分别添加点击事件监听器。
3. 在点击事件监听器中,使用 `Double.parseDouble` 方法将输入框中的字符串转换为 double 类型的数字,然后进行加减运算,并使用 `String.format` 方法将结果格式化成保留两位小数的字符串,最后显示在文本框中。
4. 如果输入的字符串无法转换为有效的数字,则弹出一个提示框。