Androidstudio计算器
时间: 2023-08-13 08:03:54 浏览: 193
如果你想开发一个简单的Android Studio计算器,可以按照以下步骤:
1. 创建一个新的Android Studio项目。
2. 在项目的`res/layout`目录下创建一个新的布局文件,例如`activity_calculator.xml`,用于展示计算器的UI界面。
3. 在布局文件中添加一个`TextView`,用于展示计算结果,以及添加多个`Button`,用于输入数字和运算符。
4. 在项目的`MainActivity`中实现计算器的逻辑。可以使用`setOnClickListener`方法为每个按钮添加点击事件,并在事件处理程序中实现计算器的逻辑。
5. 运行应用程序,测试计算器功能是否正常。
以下是一个简单的Android Studio计算器示例代码:
activity_calculator.xml:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:gravity="right"
android:textSize="32sp" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tv_result"
android:rowCount="5"
android:columnCount="4">
<Button
android:id="@+id/btn_1"
android:layout_column="0"
android:layout_row="3"
android:text="1" />
<Button
android:id="@+id/btn_2"
android:layout_column="1"
android:layout_row="3"
android:text="2" />
<Button
android:id="@+id/btn_3"
android:layout_column="2"
android:layout_row="3"
android:text="3" />
<Button
android:id="@+id/btn_add"
android:layout_column="3"
android:layout_row="3"
android:text="+" />
<Button
android:id="@+id/btn_4"
android:layout_column="0"
android:layout_row="2"
android:text="4" />
<Button
android:id="@+id/btn_5"
android:layout_column="1"
android:layout_row="2"
android:text="5" />
<Button
android:id="@+id/btn_6"
android:layout_column="2"
android:layout_row="2"
android:text="6" />
<Button
android:id="@+id/btn_subtract"
android:layout_column="3"
android:layout_row="2"
android:text="-" />
<Button
android:id="@+id/btn_7"
android:layout_column="0"
android:layout_row="1"
android:text="7" />
<Button
android:id="@+id/btn_8"
android:layout_column="1"
android:layout_row="1"
android:text="8" />
<Button
android:id="@+id/btn_9"
android:layout_column="2"
android:layout_row="1"
android:text="9" />
<Button
android:id="@+id/btn_multiply"
android:layout_column="3"
android:layout_row="1"
android:text="*" />
<Button
android:id="@+id/btn_clear"
android:layout_column="0"
android:layout_row="0"
android:layout_columnSpan="2"
android:text="C" />
<Button
android:id="@+id/btn_0"
android:layout_column="2"
android:layout_row="4"
android:text="0" />
<Button
android:id="@+id/btn_equal"
android:layout_column="3"
android:layout_row="4"
android:text="=" />
<Button
android:id="@+id/btn_divide"
android:layout_column="3"
android:layout_row="0"
android:text="/" />
</GridLayout>
</RelativeLayout>
```
MainActivity.java:
```java
public class MainActivity extends AppCompatActivity {
private TextView tvResult;
private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0;
private Button btnAdd, btnSubtract, btnMultiply, btnDivide, btnClear, btnEqual;
private String currentNumber = "";
private String lastNumber = "";
private String currentOperator = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
tvResult = findViewById(R.id.tv_result);
btn1 = findViewById(R.id.btn_1);
btn2 = findViewById(R.id.btn_2);
btn3 = findViewById(R.id.btn_3);
btn4 = findViewById(R.id.btn_4);
btn5 = findViewById(R.id.btn_5);
btn6 = findViewById(R.id.btn_6);
btn7 = findViewById(R.id.btn_7);
btn8 = findViewById(R.id.btn_8);
btn9 = findViewById(R.id.btn_9);
btn0 = findViewById(R.id.btn_0);
btnAdd = findViewById(R.id.btn_add);
btnSubtract = findViewById(R.id.btn_subtract);
btnMultiply = findViewById(R.id.btn_multiply);
btnDivide = findViewById(R.id.btn_divide);
btnClear = findViewById(R.id.btn_clear);
btnEqual = findViewById(R.id.btn_equal);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "1";
tvResult.setText(currentNumber);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "2";
tvResult.setText(currentNumber);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "3";
tvResult.setText(currentNumber);
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "4";
tvResult.setText(currentNumber);
}
});
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "5";
tvResult.setText(currentNumber);
}
});
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "6";
tvResult.setText(currentNumber);
}
});
btn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "7";
tvResult.setText(currentNumber);
}
});
btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "8";
tvResult.setText(currentNumber);
}
});
btn9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "9";
tvResult.setText(currentNumber);
}
});
btn0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber += "0";
tvResult.setText(currentNumber);
}
});
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performCalculation();
currentOperator = "+";
}
});
btnSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performCalculation();
currentOperator = "-";
}
});
btnMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performCalculation();
currentOperator = "*";
}
});
btnDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performCalculation();
currentOperator = "/";
}
});
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentNumber = "";
lastNumber = "";
currentOperator = "";
tvResult.setText("");
}
});
btnEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performCalculation();
}
});
}
private void performCalculation() {
if (!currentNumber.equals("") && !lastNumber.equals("") && !currentOperator.equals("")) {
double result = 0;
double num1 = Double.parseDouble(lastNumber);
double num2 = Double.parseDouble(currentNumber);
switch (currentOperator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
}
lastNumber = String.valueOf(result);
currentNumber = "";
currentOperator = "";
tvResult.setText(lastNumber);
} else if (!currentNumber.equals("")) {
lastNumber = currentNumber;
currentNumber = "";
}
}
}
```
这只是一个简单的示例代码,你可以根据自己的需求和想法进行更改和扩展。
阅读全文