android studio做一个计算器,写出相应代码和操作
时间: 2024-01-21 21:05:08 浏览: 95
Android studio写的计算器小程序
5星 · 资源好评率100%
好的,下面是一个简单的 Android Studio 计算器的代码和操作步骤:
1. 创建一个新的 Android Studio 项目,选择 "Empty Activity" 模板。
2. 打开 activity_main.xml 文件并添加一个 TextView 和一个 GridLayout。TextView 用于显示计算器的输入和输出,而 GridLayout 用于放置计算器的按钮。
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.calculator.MainActivity">
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="40sp" />
<GridLayout
android:layout_below="@id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="5"
android:columnCount="4"
android:orientation="horizontal">
<Button
android:layout_row="0"
android:layout_column="0"
android:text="7"
android:textSize="30sp" />
<Button
android:layout_row="0"
android:layout_column="1"
android:text="8"
android:textSize="30sp" />
<Button
android:layout_row="0"
android:layout_column="2"
android:text="9"
android:textSize="30sp" />
<Button
android:layout_row="0"
android:layout_column="3"
android:text="/"
android:textSize="30sp" />
<Button
android:layout_row="1"
android:layout_column="0"
android:text="4"
android:textSize="30sp" />
<Button
android:layout_row="1"
android:layout_column="1"
android:text="5"
android:textSize="30sp" />
<Button
android:layout_row="1"
android:layout_column="2"
android:text="6"
android:textSize="30sp" />
<Button
android:layout_row="1"
android:layout_column="3"
android:text="*"
android:textSize="30sp" />
<Button
android:layout_row="2"
android:layout_column="0"
android:text="1"
android:textSize="30sp" />
<Button
android:layout_row="2"
android:layout_column="1"
android:text="2"
android:textSize="30sp" />
<Button
android:layout_row="2"
android:layout_column="2"
android:text="3"
android:textSize="30sp" />
<Button
android:layout_row="2"
android:layout_column="3"
android:text="-"
android:textSize="30sp" />
<Button
android:layout_row="3"
android:layout_column="0"
android:text="0"
android:textSize="30sp" />
<Button
android:layout_row="3"
android:layout_column="1"
android:text="."
android:textSize="30sp" />
<Button
android:layout_row="3"
android:layout_column="2"
android:text="C"
android:textSize="30sp" />
<Button
android:layout_row="3"
android:layout_column="3"
android:text="+"
android:textSize="30sp" />
<Button
android:layout_row="4"
android:layout_column="0"
android:text="("
android:textSize="30sp" />
<Button
android:layout_row="4"
android:layout_column="1"
android:text=")"
android:textSize="30sp" />
<Button
android:layout_row="4"
android:layout_column="2"
android:text="="
android:textSize="30sp" />
<Button
android:layout_row="4"
android:layout_column="3"
android:text="^"
android:textSize="30sp" />
</GridLayout>
</RelativeLayout>
```
3. 打开 MainActivity.java 文件并添加以下代码来为按钮添加点击事件:
```java
public class MainActivity extends AppCompatActivity {
private TextView tvResult;
private String currentNumber = "";
private String leftOperand = "";
private String rightOperand = "";
private String operator = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = (TextView) findViewById(R.id.tv_result);
GridLayout gridLayout = (GridLayout) findViewById(R.id.grid_layout);
for (int i = 0; i < gridLayout.getChildCount(); i++) {
View view = gridLayout.getChildAt(i);
if (view instanceof Button) {
Button button = (Button) view;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Button button = (Button) view;
String buttonText = button.getText().toString();
switch (buttonText) {
case "C":
clear();
break;
case "+":
case "-":
case "*":
case "/":
case "^":
operator = buttonText;
leftOperand = currentNumber;
currentNumber = "";
break;
case "=":
rightOperand = currentNumber;
currentNumber = calculate();
leftOperand = "";
rightOperand = "";
operator = "";
break;
default:
currentNumber += buttonText;
break;
}
tvResult.setText(currentNumber);
}
});
}
}
}
private String calculate() {
double result = 0.0;
double left = Double.parseDouble(leftOperand);
double right = Double.parseDouble(rightOperand);
switch (operator) {
case "+":
result = left + right;
break;
case "-":
result = left - right;
break;
case "*":
result = left * right;
break;
case "/":
result = left / right;
break;
case "^":
result = Math.pow(left, right);
break;
}
return Double.toString(result);
}
private void clear() {
currentNumber = "";
leftOperand = "";
rightOperand = "";
operator = "";
tvResult.setText("");
}
}
```
4. 运行项目并在模拟器或实际设备上测试计算器。
这是一个简单的计算器,您可以根据需要进行更改和扩展。
阅读全文