android studio计算器代码
时间: 2023-09-22 18:06:03 浏览: 92
android studio计算器实现代码
以下是一个简单的 Android Studio 计算器代码,可以进行加、减、乘、除以及清零操作:
```java
public class MainActivity extends AppCompatActivity {
TextView resultTextView;
Button clearButton;
Button divideButton;
Button multiplyButton;
Button minusButton;
Button plusButton;
Button equalsButton;
Button dotButton;
Button zeroButton;
Button oneButton;
Button twoButton;
Button threeButton;
Button fourButton;
Button fiveButton;
Button sixButton;
Button sevenButton;
Button eightButton;
Button nineButton;
String operation = "";
String firstNumber = "";
String secondNumber = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView = findViewById(R.id.resultTextView);
clearButton = findViewById(R.id.clearButton);
divideButton = findViewById(R.id.divideButton);
multiplyButton = findViewById(R.id.multiplyButton);
minusButton = findViewById(R.id.minusButton);
plusButton = findViewById(R.id.plusButton);
equalsButton = findViewById(R.id.equalsButton);
dotButton = findViewById(R.id.dotButton);
zeroButton = findViewById(R.id.zeroButton);
oneButton = findViewById(R.id.oneButton);
twoButton = findViewById(R.id.twoButton);
threeButton = findViewById(R.id.threeButton);
fourButton = findViewById(R.id.fourButton);
fiveButton = findViewById(R.id.fiveButton);
sixButton = findViewById(R.id.sixButton);
sevenButton = findViewById(R.id.sevenButton);
eightButton = findViewById(R.id.eightButton);
nineButton = findViewById(R.id.nineButton);
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultTextView.setText("");
firstNumber = "";
secondNumber = "";
operation = "";
}
});
divideButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operation = "/";
if (!resultTextView.getText().toString().equals("")) {
firstNumber = resultTextView.getText().toString();
resultTextView.setText("");
}
}
});
multiplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operation = "*";
if (!resultTextView.getText().toString().equals("")) {
firstNumber = resultTextView.getText().toString();
resultTextView.setText("");
}
}
});
minusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operation = "-";
if (!resultTextView.getText().toString().equals("")) {
firstNumber = resultTextView.getText().toString();
resultTextView.setText("");
}
}
});
plusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operation = "+";
if (!resultTextView.getText().toString().equals("")) {
firstNumber = resultTextView.getText().toString();
resultTextView.setText("");
}
}
});
equalsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!firstNumber.equals("") && !resultTextView.getText().toString().equals("")) {
secondNumber = resultTextView.getText().toString();
double num1 = Double.parseDouble(firstNumber);
double num2 = Double.parseDouble(secondNumber);
double result = 0.0;
if (operation.equals("+")) {
result = num1 + num2;
} else if (operation.equals("-")) {
result = num1 - num2;
} else if (operation.equals("*")) {
result = num1 * num2;
} else if (operation.equals("/")) {
result = num1 / num2;
}
resultTextView.setText(String.valueOf(result));
}
}
});
dotButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!resultTextView.getText().toString().contains(".")) {
resultTextView.append(".");
}
}
});
zeroButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultTextView.append("0");
}
});
oneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultTextView.append("1");
}
});
twoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultTextView.append("2");
}
});
threeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultTextView.append("3");
}
});
fourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultTextView.append("4");
}
});
fiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultTextView.append("5");
}
});
sixButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultTextView.append("6");
}
});
sevenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultTextView.append("7");
}
});
eightButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultTextView.append("8");
}
});
nineButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultTextView.append("9");
}
});
}
}
```
该代码需要一个布局文件来设置计算器的 UI 界面。在 res/layout 目录下创建一个名为 activity_main.xml 的布局文件,然后添加以下代码:
```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_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:gravity="center"
android:textSize="36sp"/>
<Button
android:id="@+id/clearButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="C"
android:textSize="36sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/resultTextView"/>
<Button
android:id="@+id/divideButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="/"
android:textSize="36sp"
android:layout_toRightOf="@+id/clearButton"
android:layout_toEndOf="@+id/clearButton"
android:layout_below="@+id/resultTextView"/>
<Button
android:id="@+id/multiplyButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="*"
android:textSize="36sp"
android:layout_toRightOf="@+id/divideButton"
android:layout_toEndOf="@+id/divideButton"
android:layout_below="@+id/resultTextView"/>
<Button
android:id="@+id/minusButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="-"
android:textSize="36sp"
android:layout_toRightOf="@+id/multiplyButton"
android:layout_toEndOf="@+id/multiplyButton"
android:layout_below="@+id/resultTextView"/>
<Button
android:id="@+id/plusButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="+"
android:textSize="36sp"
android:layout_toRightOf="@+id/minusButton"
android:layout_toEndOf="@+id/minusButton"
android:layout_below="@+id/resultTextView"/>
<Button
android:id="@+id/equalsButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="="
android:textSize="36sp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/resultTextView"/>
<Button
android:id="@+id/dotButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="."
android:textSize="36sp"
android:layout_toLeftOf="@+id/zeroButton"
android:layout_above="@+id/equalsButton"/>
<Button
android:id="@+id/zeroButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="0"
android:textSize="36sp"
android:layout_above="@+id/equalsButton"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/oneButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="1"
android:textSize="36sp"
android:layout_above="@+id/fourButton"
android:layout_alignLeft="@+id/zeroButton"
android:layout_alignStart="@+id/zeroButton"/>
<Button
android:id="@+id/twoButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="2"
android:textSize="36sp"
android:layout_above="@+id/fiveButton"
android:layout_toRightOf="@+id/oneButton"
android:layout_toEndOf="@+id/oneButton"/>
<Button
android:id="@+id/threeButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="3"
android:textSize="36sp"
android:layout_above="@+id/sixButton"
android:layout_toRightOf="@+id/twoButton"
android:layout_toEndOf="@+id/twoButton"/>
<Button
android:id="@+id/fourButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="4"
android:textSize="36sp"
android:layout_above="@+id/sevenButton"
android:layout_alignLeft="@+id/oneButton"
android:layout_alignStart="@+id/oneButton"/>
<Button
android:id="@+id/fiveButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="5"
android:textSize="36sp"
android:layout_above="@+id/eightButton"
android:layout_toRightOf="@+id/fourButton"
android:layout_toEndOf="@+id/fourButton"/>
<Button
android:id="@+id/sixButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="6"
android:textSize="36sp"
android:layout_above="@+id/nineButton"
android:layout_toRightOf="@+id/fiveButton"
android:layout_toEndOf="@+id/fiveButton"/>
<Button
android:id="@+id/sevenButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="7"
android:textSize="36sp"
android:layout_alignLeft="@+id/fourButton"
android:layout_alignStart="@+id/fourButton"
android:layout_above="@+id/clearButton"/>
<Button
android:id="@+id/eightButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="8"
android:textSize="36sp"
android:layout_alignLeft="@+id/fiveButton"
android:layout_alignStart="@+id/fiveButton"
android:layout_above="@+id/clearButton"
android:layout_toRightOf="@+id/sevenButton"
android:layout_toEndOf="@+id/sevenButton"/>
<Button
android:id="@+id/nineButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="9"
android:textSize="36sp"
android:layout_alignLeft="@+id/sixButton"
android:layout_alignStart="@+id/sixButton"
android:layout_above="@+id/clearButton"
android:layout_toRightOf="@+id/eightButton"
android:layout_toEndOf="@+id/eightButton"/>
</RelativeLayout>
```
阅读全文