Android Studio 计算器代码
时间: 2023-11-02 22:08:48 浏览: 146
android studio计算器实现代码
以下是一个简单的 Android Studio 计算器代码:
XML 布局文件:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:gravity="right"
android:padding="8dp"
android:text="0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8" />
<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9" />
<Button
android:id="@+id/buttonDivide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
<Button
android:id="@+id/buttonMultiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<Button
android:id="@+id/buttonMinus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0" />
<Button
android:id="@+id/buttonDot"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="." />
<Button
android:id="@+id/buttonEquals"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="=" />
<Button
android:id="@+id/buttonPlus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+" />
</LinearLayout>
<Button
android:id="@+id/buttonClear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clear" />
</LinearLayout>
```
Java 代码:
```java
public class MainActivity extends AppCompatActivity {
private TextView textView;
private String currentNumber = "0";
private String currentOperator = "";
private boolean operatorClicked = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
Button button0 = findViewById(R.id.button0);
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberClicked("0");
}
});
Button button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberClicked("1");
}
});
Button button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberClicked("2");
}
});
Button button3 = findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberClicked("3");
}
});
Button button4 = findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberClicked("4");
}
});
Button button5 = findViewById(R.id.button5);
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberClicked("5");
}
});
Button button6 = findViewById(R.id.button6);
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberClicked("6");
}
});
Button button7 = findViewById(R.id.button7);
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberClicked("7");
}
});
Button button8 = findViewById(R.id.button8);
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberClicked("8");
}
});
Button button9 = findViewById(R.id.button9);
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberClicked("9");
}
});
Button buttonDot = findViewById(R.id.buttonDot);
buttonDot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!currentNumber.contains(".")) {
currentNumber += ".";
textView.setText(currentNumber);
}
}
});
Button buttonPlus = findViewById(R.id.buttonPlus);
buttonPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operatorClicked("+");
}
});
Button buttonMinus = findViewById(R.id.buttonMinus);
buttonMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operatorClicked("-");
}
});
Button buttonMultiply = findViewById(R.id.buttonMultiply);
buttonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operatorClicked("*");
}
});
Button buttonDivide = findViewById(R.id.buttonDivide);
buttonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operatorClicked("/");
}
});
Button buttonEquals = findViewById(R.id.buttonEquals);
buttonEquals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!operatorClicked) {
return;
}
double result = 0;
switch (currentOperator) {
case "+":
result = Double.parseDouble(currentNumber) + Double.parseDouble(textView.getText().toString());
break;
case "-":
result = Double.parseDouble(currentNumber) - Double.parseDouble(textView.getText().toString());
break;
case "*":
result = Double.parseDouble(currentNumber) * Double.parseDouble(textView.getText().toString());
break;
case "/":
result = Double.parseDouble(currentNumber) / Double.parseDouble(textView.getText().toString());
break;
}
currentNumber = String.valueOf(result);
textView.setText(currentNumber);
operatorClicked = false;
}
});
Button buttonClear = findViewById(R.id.buttonClear);
buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber = "0";
currentOperator = "";
operatorClicked = false;
textView.setText(currentNumber);
}
});
}
private void numberClicked(String number) {
if (currentNumber.equals("0")) {
currentNumber = number;
} else {
currentNumber += number;
}
textView.setText(currentNumber);
operatorClicked = true;
}
private void operatorClicked(String operator) {
if (!operatorClicked) {
currentOperator = operator;
operatorClicked = true;
} else {
double result = 0;
switch (currentOperator) {
case "+":
result = Double.parseDouble(currentNumber) + Double.parseDouble(textView.getText().toString());
break;
case "-":
result = Double.parseDouble(currentNumber) - Double.parseDouble(textView.getText().toString());
break;
case "*":
result = Double.parseDouble(currentNumber) * Double.parseDouble(textView.getText().toString());
break;
case "/":
result = Double.parseDouble(currentNumber) / Double.parseDouble(textView.getText().toString());
break;
}
currentNumber = String.valueOf(result);
textView.setText(currentNumber);
currentOperator = operator;
}
}
}
```
阅读全文