android studio 计算器代码
时间: 2023-09-14 22:08:44 浏览: 128
android studio实现计算器
5星 · 资源好评率100%
以下是一个简单的 Android Studio 计算器应用程序的代码示例:
1. 创建一个新项目,命名为 Calculator。
2. 在 activity_main.xml 布局文件中添加一个 TextView 和多个 Button,如下所示:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:padding="16dp"
android:textSize="24sp"
android:textStyle="bold"
android:text="0"
android:gravity="end" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_below="@+id/tvResult"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:onClick="onDigitClick" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_below="@+id/tvResult"
android:layout_toRightOf="@+id/btn1"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:onClick="onDigitClick" />
<!-- Add more buttons for digits, operators, and clear/calculate buttons -->
</RelativeLayout>
```
3. 在 MainActivity.java 文件中添加以下代码:
```
public class MainActivity extends AppCompatActivity {
private TextView tvResult;
private String currentNumber = "0";
private String lastOperator = "";
private double operand1 = 0;
private double operand2 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = findViewById(R.id.tvResult);
}
public void onDigitClick(View view) {
String digit = ((Button) view).getText().toString();
if (currentNumber.equals("0")) {
currentNumber = digit;
} else {
currentNumber += digit;
}
tvResult.setText(currentNumber);
}
public void onOperatorClick(View view) {
String operator = ((Button) view).getText().toString();
if (!lastOperator.isEmpty()) {
operand2 = Double.parseDouble(currentNumber);
currentNumber = calculate();
tvResult.setText(currentNumber);
} else {
operand1 = Double.parseDouble(currentNumber);
}
currentNumber = "0";
lastOperator = operator;
}
public void onClearClick(View view) {
currentNumber = "0";
lastOperator = "";
operand1 = 0;
operand2 = 0;
tvResult.setText(currentNumber);
}
public void onCalculateClick(View view) {
if (!lastOperator.isEmpty()) {
operand2 = Double.parseDouble(currentNumber);
currentNumber = calculate();
tvResult.setText(currentNumber);
}
}
private String calculate() {
double result = 0;
switch (lastOperator) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*":
result = operand1 * operand2;
break;
case "/":
result = operand1 / operand2;
break;
}
operand1 = result;
return Double.toString(result);
}
}
```
这个计算器应用程序中的核心逻辑是在 onDigitClick、onOperatorClick、onClearClick 和 onCalculateClick 方法中。这些方法会根据用户点击的按钮执行相应的操作,例如添加数字、执行计算等。calculate 方法用于计算两个操作数之间的结果。
注意:此处仅提供一个简单的计算器应用程序示例,你可以根据自己的需求进行修改并添加更多的功能。
阅读全文