帮我用androidStudio写一个简单计算器页面和代码
时间: 2024-01-22 07:18:43 浏览: 85
Android Studio实现简单计算器APP
好的,下面是一个简单的计算器页面和代码示例:
1. 首先,在Android Studio中创建一个新项目,选择Empty Activity。
2. 打开activity_main.xml文件,将以下代码复制并粘贴到文件中:
```xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/resultTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="0"
android:textAlignment="center"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/clearButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:backgroundTint="#FF0000"
android:text="C"
app:layout_constraintEnd_toStartOf="@+id/divideButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/resultTextView" />
<Button
android:id="@+id/divideButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="/"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/clearButton"
app:layout_constraintTop_toBottomOf="@+id/resultTextView" />
<Button
android:id="@+id/multiplyButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="*"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/divideButton"
app:layout_constraintTop_toBottomOf="@+id/resultTextView" />
<Button
android:id="@+id/minusButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="-"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/multiplyButton"
app:layout_constraintTop_toBottomOf="@+id/resultTextView" />
<Button
android:id="@+id/plusButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="+"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/minusButton"
app:layout_constraintTop_toBottomOf="@+id/resultTextView" />
<Button
android:id="@+id/equalButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:backgroundTint="#00FF00"
android:text="="
app:layout_constraintEnd_toStartOf="@+id/plusButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divideButton" />
<Button
android:id="@+id/dotButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/equalButton"
app:layout_constraintTop_toBottomOf="@+id/divideButton" />
<Button
android:id="@+id/zeroButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="0"
app:layout_constraintEnd_toStartOf="@+id/dotButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/equalButton"
app:layout_constraintTop_toBottomOf="@+id/divideButton" />
<Button
android:id="@+id/oneButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="1"
app:layout_constraintEnd_toStartOf="@+id/twoButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/equalButton" />
<Button
android:id="@+id/twoButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="2"
app:layout_constraintEnd_toStartOf="@+id/threeButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/oneButton"
app:layout_constraintTop_toBottomOf="@+id/equalButton" />
<Button
android:id="@+id/threeButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/twoButton"
app:layout_constraintTop_toBottomOf="@+id/equalButton" />
<Button
android:id="@+id/fourButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="4"
app:layout_constraintEnd_toStartOf="@+id/fiveButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/oneButton" />
<Button
android:id="@+id/fiveButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="5"
app:layout_constraintEnd_toStartOf="@+id/sixButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/fourButton"
app:layout_constraintTop_toBottomOf="@+id/twoButton" />
<Button
android:id="@+id/sixButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/fiveButton"
app:layout_constraintTop_toBottomOf="@+id/threeButton" />
<Button
android:id="@+id/sevenButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="7"
app:layout_constraintEnd_toStartOf="@+id/eightButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/fourButton" />
<Button
android:id="@+id/eightButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="8"
app:layout_constraintEnd_toStartOf="@+id/nineButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/sevenButton"
app:layout_constraintTop_toBottomOf="@+id/fiveButton" />
<Button
android:id="@+id/nineButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/eightButton"
app:layout_constraintTop_toBottomOf="@+id/sixButton" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="64dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="312dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
```
3. 打开MainActivity.java文件,将以下代码复制并粘贴到文件中:
```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 {
private TextView resultTextView;
private String display = "";
private double num1 = 0;
private double num2 = 0;
private char operation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView = findViewById(R.id.resultTextView);
Button clearButton = findViewById(R.id.clearButton);
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
display = "";
num1 = 0;
num2 = 0;
operation = ' ';
resultTextView.setText("0");
}
});
Button divideButton = findViewById(R.id.divideButton);
divideButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operation = '/';
num1 = Double.parseDouble(display);
display = "";
}
});
Button multiplyButton = findViewById(R.id.multiplyButton);
multiplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operation = '*';
num1 = Double.parseDouble(display);
display = "";
}
});
Button minusButton = findViewById(R.id.minusButton);
minusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operation = '-';
num1 = Double.parseDouble(display);
display = "";
}
});
Button plusButton = findViewById(R.id.plusButton);
plusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operation = '+';
num1 = Double.parseDouble(display);
display = "";
}
});
Button equalButton = findViewById(R.id.equalButton);
equalButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
num2 = Double.parseDouble(display);
double result = 0;
switch (operation) {
case '/':
if (num2 == 0) {
resultTextView.setText("Error");
} else {
result = num1 / num2;
resultTextView.setText(String.valueOf(result));
}
break;
case '*':
result = num1 * num2;
resultTextView.setText(String.valueOf(result));
break;
case '-':
result = num1 - num2;
resultTextView.setText(String.valueOf(result));
break;
case '+':
result = num1 + num2;
resultTextView.setText(String.valueOf(result));
break;
}
}
});
Button dotButton = findViewById(R.id.dotButton);
dotButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!display.contains(".")) {
display += ".";
resultTextView.setText(display);
}
}
});
Button zeroButton = findViewById(R.id.zeroButton);
zeroButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
display += "0";
resultTextView.setText(display);
}
});
Button oneButton = findViewById(R.id.oneButton);
oneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
display += "1";
resultTextView.setText(display);
}
});
Button twoButton = findViewById(R.id.twoButton);
twoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
display += "2";
resultTextView.setText(display);
}
});
Button threeButton = findViewById(R.id.threeButton);
threeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
display += "3";
resultTextView.setText(display);
}
});
Button fourButton = findViewById(R.id.fourButton);
fourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
display += "4";
resultTextView.setText(display);
}
});
Button fiveButton = findViewById(R.id.fiveButton);
fiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
display += "5";
resultTextView.setText(display);
}
});
Button sixButton = findViewById(R.id.sixButton);
sixButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
display += "6";
resultTextView.setText(display);
}
});
Button sevenButton = findViewById(R.id.sevenButton);
sevenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
display += "7";
resultTextView.setText(display);
}
});
Button eightButton = findViewById(R.id.eightButton);
eightButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
display += "8";
resultTextView.setText(display);
}
});
Button nineButton = findViewById(R.id.nineButton);
nineButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
display += "9";
resultTextView.setText(display);
}
});
}
}
```
4. 运行应用程序,您现在可以在模拟器或设备上测试它了。
阅读全文