Androidstudio生成一个计算器代码
时间: 2024-05-01 08:23:13 浏览: 151
基于Android Studio 开发计算器代码
以下是一个简单的 Android Studio 计算器应用程序的代码示例:
1. 创建一个新的 Android Studio 项目并设置基本信息(包名称、应用名称等)。
2. 打开 activity_main.xml 文件,并将以下代码添加到其中:
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:layout_margin="16dp"
android:background="@color/colorPrimaryDark"
android:gravity="right"
android:textColor="@android:color/white"
android:textSize="32sp"
android:textStyle="bold" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/resultTextView"
android:columnCount="4"
android:rowCount="5"
android:orientation="horizontal">
<Button
android:id="@+id/buttonClear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="C"
android:textSize="24sp" />
<Button
android:id="@+id/buttonBackspace"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="<-"
android:textSize="24sp" />
<Button
android:id="@+id/buttonPercent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="%"
android:textSize="24sp" />
<Button
android:id="@+id/buttonDivide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="/"
android:textSize="24sp" />
<Button
android:id="@+id/buttonSeven"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="7"
android:textSize="24sp" />
<Button
android:id="@+id/buttonEight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="8"
android:textSize="24sp" />
<Button
android:id="@+id/buttonNine"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="9"
android:textSize="24sp" />
<Button
android:id="@+id/buttonMultiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="*"
android:textSize="24sp" />
<Button
android:id="@+id/buttonFour"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="4"
android:textSize="24sp" />
<Button
android:id="@+id/buttonFive"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="5"
android:textSize="24sp" />
<Button
android:id="@+id/buttonSix"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="6"
android:textSize="24sp" />
<Button
android:id="@+id/buttonMinus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="-"
android:textSize="24sp" />
<Button
android:id="@+id/buttonOne"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="1"
android:textSize="24sp" />
<Button
android:id="@+id/buttonTwo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="2"
android:textSize="24sp" />
<Button
android:id="@+id/buttonThree"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="3"
android:textSize="24sp" />
<Button
android:id="@+id/buttonPlus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="+"
android:textSize="24sp" />
<Button
android:id="@+id/buttonNegative"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="+/-"
android:textSize="24sp" />
<Button
android:id="@+id/buttonZero"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="0"
android:textSize="24sp" />
<Button
android:id="@+id/buttonDot"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="."
android:textSize="24sp" />
<Button
android:id="@+id/buttonEquals"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="2"
android:text="="
android:textSize="24sp" />
</GridLayout>
</RelativeLayout>
```
3. 打开 MainActivity.java 文件,并将以下代码添加到其中:
```
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView resultTextView;
private Button buttonClear, buttonBackspace, buttonPercent, buttonDivide, buttonSeven,
buttonEight, buttonNine, buttonMultiply, buttonFour, buttonFive, buttonSix,
buttonMinus, buttonOne, buttonTwo, buttonThree, buttonPlus, buttonNegative,
buttonZero, buttonDot, buttonEquals;
private String currentNumber = "";
private String operator = "";
private double result = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView = findViewById(R.id.resultTextView);
buttonClear = findViewById(R.id.buttonClear);
buttonBackspace = findViewById(R.id.buttonBackspace);
buttonPercent = findViewById(R.id.buttonPercent);
buttonDivide = findViewById(R.id.buttonDivide);
buttonSeven = findViewById(R.id.buttonSeven);
buttonEight = findViewById(R.id.buttonEight);
buttonNine = findViewById(R.id.buttonNine);
buttonMultiply = findViewById(R.id.buttonMultiply);
buttonFour = findViewById(R.id.buttonFour);
buttonFive = findViewById(R.id.buttonFive);
buttonSix = findViewById(R.id.buttonSix);
buttonMinus = findViewById(R.id.buttonMinus);
buttonOne = findViewById(R.id.buttonOne);
buttonTwo = findViewById(R.id.buttonTwo);
buttonThree = findViewById(R.id.buttonThree);
buttonPlus = findViewById(R.id.buttonPlus);
buttonNegative = findViewById(R.id.buttonNegative);
buttonZero = findViewById(R.id.buttonZero);
buttonDot = findViewById(R.id.buttonDot);
buttonEquals = findViewById(R.id.buttonEquals);
buttonClear.setOnClickListener(this);
buttonBackspace.setOnClickListener(this);
buttonPercent.setOnClickListener(this);
buttonDivide.setOnClickListener(this);
buttonSeven.setOnClickListener(this);
buttonEight.setOnClickListener(this);
buttonNine.setOnClickListener(this);
buttonMultiply.setOnClickListener(this);
buttonFour.setOnClickListener(this);
buttonFive.setOnClickListener(this);
buttonSix.setOnClickListener(this);
buttonMinus.setOnClickListener(this);
buttonOne.setOnClickListener(this);
buttonTwo.setOnClickListener(this);
buttonThree.setOnClickListener(this);
buttonPlus.setOnClickListener(this);
buttonNegative.setOnClickListener(this);
buttonZero.setOnClickListener(this);
buttonDot.setOnClickListener(this);
buttonEquals.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonClear:
currentNumber = "";
operator = "";
result = 0;
resultTextView.setText("");
break;
case R.id.buttonBackspace:
if (currentNumber.length() > 0) {
currentNumber = currentNumber.substring(0, currentNumber.length() - 1);
resultTextView.setText(currentNumber);
}
break;
case R.id.buttonPercent:
if (currentNumber.length() > 0) {
double percentValue = Double.parseDouble(currentNumber) / 100;
currentNumber = String.valueOf(percentValue);
resultTextView.setText(currentNumber);
}
break;
case R.id.buttonDivide:
case R.id.buttonMultiply:
case R.id.buttonMinus:
case R.id.buttonPlus:
if (operator.isEmpty()) {
operator = ((Button) v).getText().toString();
result = Double.parseDouble(currentNumber);
currentNumber = "";
} else {
calculate();
operator = ((Button) v).getText().toString();
}
break;
case R.id.buttonEquals:
calculate();
break;
default:
currentNumber += ((Button) v).getText().toString();
resultTextView.setText(currentNumber);
break;
}
}
private void calculate() {
if (currentNumber.length() > 0) {
double number = Double.parseDouble(currentNumber);
switch (operator) {
case "/":
result /= number;
break;
case "*":
result *= number;
break;
case "-":
result -= number;
break;
case "+":
result += number;
break;
}
currentNumber = String.valueOf(result);
resultTextView.setText(currentNumber);
operator = "";
}
}
}
```
4. 运行应用程序并测试它。现在你有了一个简单的 Android Studio 计算器应用程序!
阅读全文