androidstudio实现计算器,生成代码
时间: 2023-05-19 15:07:00 浏览: 171
非常感谢您的提问。以下是一个简单的 Android Studio 计算器的代码示例:
activity_main.xml:
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/resultTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:gravity="end"
android:padding="16dp"
android:text="0" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="5"
android:columnCount="4">
<Button
android:id="@+id/buttonClear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:layout_columnWeight="1"
android:text="C" />
<Button
android:id="@+id/buttonSign"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="1"
android:layout_columnWeight="1"
android:text="+/-" />
<Button
android:id="@+id/buttonPercent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="2"
android:layout_columnWeight="1"
android:text="%" />
<Button
android:id="@+id/buttonDivide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="3"
android:layout_columnWeight="1"
android:text="/" />
<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:layout_columnWeight="1"
android:text="7" />
<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="1"
android:layout_columnWeight="1"
android:text="8" />
<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="2"
android:layout_columnWeight="1"
android:text="9" />
<Button
android:id="@+id/buttonMultiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="3"
android:layout_columnWeight="1"
android:text="*" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="0"
android:layout_columnWeight="1"
android:text="4" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="1"
android:layout_columnWeight="1"
android:text="5" />
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="2"
android:layout_columnWeight="1"
android:text="6" />
<Button
android:id="@+id/buttonSubtract"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="3"
android:layout_columnWeight="1"
android:text="-" />
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="0"
android:layout_columnWeight="1"
android:text="1" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="1"
android:layout_columnWeight="1"
android:text="2" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="2"
android:layout_columnWeight="1"
android:text="3" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="3"
android:layout_columnWeight="1"
android:text="+" />
<Button
android:id="@+id/button0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_columnWeight="2"
android:text="0" />
<Button
android:id="@+id/buttonDecimal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="2"
android:layout_columnWeight="1"
android:text="." />
<Button
android:id="@+id/buttonEquals"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="3"
android:layout_columnWeight="1"
android:text="=" />
</GridLayout>
</LinearLayout>
```
MainActivity.java:
```
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
private TextView resultTextView;
private String currentNumber = "";
private String lastOperation = "";
private double result = 0;
private DecimalFormat decimalFormat = new DecimalFormat("#.##########");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView = findViewById(R.id.resultTextView);
Button buttonClear = findViewById(R.id.buttonClear);
buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clear();
}
});
Button buttonSign = findViewById(R.id.buttonSign);
buttonSign.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sign();
}
});
Button buttonPercent = findViewById(R.id.buttonPercent);
buttonPercent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
percent();
}
});
Button buttonDivide = findViewById(R.id.buttonDivide);
buttonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operation("/");
}
});
Button buttonMultiply = findViewById(R.id.buttonMultiply);
buttonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operation("*");
}
});
Button buttonSubtract = findViewById(R.id.buttonSubtract);
buttonSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operation("-");
}
});
Button buttonAdd = findViewById(R.id.buttonAdd);
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operation("+");
}
});
Button buttonEquals = findViewById(R.id.buttonEquals);
buttonEquals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
equals();
}
});
Button button0 = findViewById(R.id.button0);
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
number("0");
}
});
Button button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
number("1");
}
});
Button button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
number("2");
}
});
Button button3 = findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
number("3");
}
});
Button button4 = findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
number("4");
}
});
Button button5 = findViewById(R.id.button5);
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
number("5");
}
});
Button button6 = findViewById(R.id.button6);
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
number("6");
}
});
Button button7 = findViewById(R.id.button7);
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
number("7");
}
});
Button button8 = findViewById(R.id.button8);
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
number("8");
}
});
Button button9 = findViewById(R.id.button9);
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
number("9");
}
});
Button buttonDecimal = findViewById(R.id.buttonDecimal);
buttonDecimal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
decimal();
}
});
}
private void clear() {
currentNumber = "";
lastOperation = "";
result = 0;
resultTextView.setText("0");
}
private void sign() {
if (!currentNumber.isEmpty()) {
double number = Double.parseDouble(currentNumber);
number = -number;
currentNumber = decimalFormat.format(number);
resultTextView.setText(currentNumber);
}
}
private void percent() {
if (!currentNumber.isEmpty()) {
double number = Double.parseDouble(currentNumber);
number = number / 100;
currentNumber = decimalFormat.format(number);
resultTextView.setText(currentNumber);
}
}
private void operation(String operation) {
if (!currentNumber.isEmpty()) {
if (!lastOperation.isEmpty()) {
equals();
}
result = Double.parseDouble(currentNumber);
currentNumber = "";
lastOperation = operation;
}
}
private void equals() {
if (!lastOperation.isEmpty() && !currentNumber.isEmpty()) {
double number = Double.parseDouble(currentNumber);
switch (lastOperation) {
case "+":
result += number;
break;
case "-":
result -= number;
break;
case "*":
result *= number;
break;
case "/":
result /= number;
break;
}
currentNumber = decimalFormat.format(result);
resultTextView.setText(currentNumber);
lastOperation = "";
}
}
private void number(String number) {
currentNumber += number;
resultTextView.setText(currentNumber);
}
private void decimal() {
if (!currentNumber.contains(".")) {
currentNumber += ".";
resultTextView.setText(currentNumber);
}
}
}
```
希望这个示例能够帮助您实现一个简单的 Android Studio 计算器。
阅读全文