在android studio中制作一个没有优先级,运行结果保留两位小数的计算器
时间: 2023-12-14 10:37:40 浏览: 77
好的,我们可以通过简单的布局和代码来实现这个计算器。
首先,我们需要创建一个新的Android Studio项目,并在activity_main.xml文件中添加以下代码:
```
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="0"
android:textSize="30sp" />
<LinearLayout
android:layout_below="@id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4"
android:paddingTop="10dp">
<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7"
android:textSize="20sp" />
<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8"
android:textSize="20sp" />
<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="9"
android:textSize="20sp" />
<Button
android:id="@+id/button_divide"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="/"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_below="@id/button7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4"
android:paddingTop="10dp">
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
android:textSize="20sp" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5"
android:textSize="20sp" />
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6"
android:textSize="20sp" />
<Button
android:id="@+id/button_multiply"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="*"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_below="@id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4"
android:paddingTop="10dp">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
android:textSize="20sp" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
android:textSize="20sp" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"
android:textSize="20sp" />
<Button
android:id="@+id/button_minus"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="-"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_below="@id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4"
android:paddingTop="10dp">
<Button
android:id="@+id/button_dot"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="."
android:textSize="20sp" />
<Button
android:id="@+id/button0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0"
android:textSize="20sp" />
<Button
android:id="@+id/button_equal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="="
android:textSize="20sp" />
<Button
android:id="@+id/button_plus"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="+"
android:textSize="20sp" />
</LinearLayout>
<Button
android:id="@+id/button_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_dot"
android:layout_alignParentEnd="true"
android:text="C"
android:textSize="20sp" />
</RelativeLayout>
```
上面的代码中,我们使用了RelativeLayout作为根布局,并在其中添加了一个EditText和若干个Button,用于展示数字、运算符和结果。
接下来,在MainActivity.java文件中添加以下代码:
```
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button button0, button1, button2, button3, button4,
button5, button6, button7, button8, button9, buttonClear,
buttonPlus, buttonMinus, buttonMultiply, buttonDivide, buttonEqual, buttonDot;
private String currentNumber = "";
private String operator = "";
private double result = 0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
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);
buttonClear = findViewById(R.id.button_clear);
buttonPlus = findViewById(R.id.button_plus);
buttonMinus = findViewById(R.id.button_minus);
buttonMultiply = findViewById(R.id.button_multiply);
buttonDivide = findViewById(R.id.button_divide);
buttonEqual = findViewById(R.id.button_equal);
buttonDot = findViewById(R.id.button_dot);
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "0";
editText.setText(currentNumber);
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "1";
editText.setText(currentNumber);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "2";
editText.setText(currentNumber);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "3";
editText.setText(currentNumber);
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "4";
editText.setText(currentNumber);
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "5";
editText.setText(currentNumber);
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "6";
editText.setText(currentNumber);
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "7";
editText.setText(currentNumber);
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "8";
editText.setText(currentNumber);
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "9";
editText.setText(currentNumber);
}
});
buttonDot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!currentNumber.contains(".")) {
currentNumber += ".";
editText.setText(currentNumber);
}
}
});
buttonPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!currentNumber.equals("")) {
if (!operator.equals("")) {
calculate();
}
operator = "+";
result = Double.parseDouble(currentNumber);
currentNumber = "";
}
}
});
buttonMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!currentNumber.equals("")) {
if (!operator.equals("")) {
calculate();
}
operator = "-";
result = Double.parseDouble(currentNumber);
currentNumber = "";
}
}
});
buttonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!currentNumber.equals("")) {
if (!operator.equals("")) {
calculate();
}
operator = "*";
result = Double.parseDouble(currentNumber);
currentNumber = "";
}
}
});
buttonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!currentNumber.equals("")) {
if (!operator.equals("")) {
calculate();
}
operator = "/";
result = Double.parseDouble(currentNumber);
currentNumber = "";
}
}
});
buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber = "";
operator = "";
result = 0.0;
editText.setText("0");
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!currentNumber.equals("") && !operator.equals("")) {
calculate();
operator = "";
currentNumber = String.format("%.2f", result);
editText.setText(currentNumber);
}
}
});
}
private void calculate() {
double number = Double.parseDouble(currentNumber);
switch (operator) {
case "+":
result += number;
break;
case "-":
result -= number;
break;
case "*":
result *= number;
break;
case "/":
if (number != 0) {
result /= number;
}
break;
}
}
}
```
上面的代码中,我们首先声明了一些变量,包括当前输入的数字、当前运算符和计算结果。然后,在onCreate方法中,我们获取了EditText和若干个Button,并为这些Button设置了点击事件。
在点击数字Button时,我们将当前数字添加到currentNumber中,并更新EditText的内容。在点击运算符Button时,我们首先判断是否已经输入了数字,如果是,则进行计算并更新result和operator的值。在点击C清空Button时,我们将所有变量都重置为初始值。在点击等号Button时,我们先进行计算,然后将结果保留两位小数,并更新EditText的内容。
最后,记得在AndroidManifest.xml文件中添加以下代码:
```
<uses-permission android:name="android.permission.INTERNET" />
```
这样我们就完成了一个简单的计算器应用,它没有优先级,运行结果保留两位小数。
阅读全文