Android studio制作简易计算器
时间: 2023-11-24 10:08:23 浏览: 100
Android studio实现简单计算器
5星 · 资源好评率100%
好的,下面是制作简易计算器的步骤:
1. 打开Android Studio,创建一个新项目。
2. 在布局文件中添加一个EditText用于显示计算结果和多个Button用于输入数字和运算符。
3. 在MainActivity中获取EditText和Button的引用,并设置Button的点击事件。
4. 在点击事件中,根据按钮的标识符进行不同的计算,并将结果显示到EditText中。
5. 完成以上步骤后,就可以运行程序并测试计算器的功能了。
下面是一个简单的例子代码,供参考:
activity_main.xml:
```
<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"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:hint="0"
android:padding="16dp"
android:textAlignment="textEnd"
android:textSize="24sp" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="4"
android:columnCount="4"
android:layout_below="@id/result">
<Button
android:id="@+id/button_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="1"
android:textSize="24sp" />
<Button
android:id="@+id/button_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="2"
android:textSize="24sp" />
<Button
android:id="@+id/button_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="3"
android:textSize="24sp" />
<Button
android:id="@+id/button_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="+"
android:textSize="24sp" />
<Button
android:id="@+id/button_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="4"
android:textSize="24sp" />
<Button
android:id="@+id/button_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="5"
android:textSize="24sp" />
<Button
android:id="@+id/button_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="6"
android:textSize="24sp" />
<Button
android:id="@+id/button_subtract"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="-"
android:textSize="24sp" />
<Button
android:id="@+id/button_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="7"
android:textSize="24sp" />
<Button
android:id="@+id/button_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="8"
android:textSize="24sp" />
<Button
android:id="@+id/button_9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="9"
android:textSize="24sp" />
<Button
android:id="@+id/button_multiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="*"
android:textSize="24sp" />
<Button
android:id="@+id/button_clear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="C"
android:textSize="24sp" />
<Button
android:id="@+id/button_0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="0"
android:textSize="24sp" />
<Button
android:id="@+id/button_equal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="="
android:textSize="24sp" />
<Button
android:id="@+id/button_divide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="/"
android:textSize="24sp" />
</GridLayout>
</RelativeLayout>
```
MainActivity.java:
```
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText result;
private Button button_0, button_1, button_2, button_3, button_4, button_5, button_6, button_7, button_8, button_9;
private Button button_add, button_subtract, button_multiply, button_divide, button_clear, button_equal;
private boolean isAddition, isSubtraction, isMultiplication, isDivision;
private float value1, value2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = findViewById(R.id.result);
button_0 = findViewById(R.id.button_0);
button_1 = findViewById(R.id.button_1);
button_2 = findViewById(R.id.button_2);
button_3 = findViewById(R.id.button_3);
button_4 = findViewById(R.id.button_4);
button_5 = findViewById(R.id.button_5);
button_6 = findViewById(R.id.button_6);
button_7 = findViewById(R.id.button_7);
button_8 = findViewById(R.id.button_8);
button_9 = findViewById(R.id.button_9);
button_add = findViewById(R.id.button_add);
button_subtract = findViewById(R.id.button_subtract);
button_multiply = findViewById(R.id.button_multiply);
button_divide = findViewById(R.id.button_divide);
button_clear = findViewById(R.id.button_clear);
button_equal = findViewById(R.id.button_equal);
button_0.setOnClickListener(this);
button_1.setOnClickListener(this);
button_2.setOnClickListener(this);
button_3.setOnClickListener(this);
button_4.setOnClickListener(this);
button_5.setOnClickListener(this);
button_6.setOnClickListener(this);
button_7.setOnClickListener(this);
button_8.setOnClickListener(this);
button_9.setOnClickListener(this);
button_add.setOnClickListener(this);
button_subtract.setOnClickListener(this);
button_multiply.setOnClickListener(this);
button_divide.setOnClickListener(this);
button_clear.setOnClickListener(this);
button_equal.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button_0:
result.setText(result.getText() + "0");
break;
case R.id.button_1:
result.setText(result.getText() + "1");
break;
case R.id.button_2:
result.setText(result.getText() + "2");
break;
case R.id.button_3:
result.setText(result.getText() + "3");
break;
case R.id.button_4:
result.setText(result.getText() + "4");
break;
case R.id.button_5:
result.setText(result.getText() + "5");
break;
case R.id.button_6:
result.setText(result.getText() + "6");
break;
case R.id.button_7:
result.setText(result.getText() + "7");
break;
case R.id.button_8:
result.setText(result.getText() + "8");
break;
case R.id.button_9:
result.setText(result.getText() + "9");
break;
case R.id.button_add:
if (result == null) {
result.setText("");
} else {
value1 = Float.parseFloat(result.getText() + "");
isAddition = true;
result.setText(null);
}
break;
case R.id.button_subtract:
if (result == null) {
result.setText("");
} else {
value1 = Float.parseFloat(result.getText() + "");
isSubtraction = true;
result.setText(null);
}
break;
case R.id.button_multiply:
if (result == null) {
result.setText("");
} else {
value1 = Float.parseFloat(result.getText() + "");
isMultiplication = true;
result.setText(null);
}
break;
case R.id.button_divide:
if (result == null) {
result.setText("");
} else {
value1 = Float.parseFloat(result.getText() + "");
isDivision = true;
result.setText(null);
}
break;
case R.id.button_clear:
result.setText("");
break;
case R.id.button_equal:
value2 = Float.parseFloat(result.getText() + "");
if (isAddition) {
result.setText(value1 + value2 + "");
isAddition = false;
}
if (isSubtraction) {
result.setText(value1 - value2 + "");
isSubtraction = false;
}
if (isMultiplication) {
result.setText(value1 * value2 + "");
isMultiplication = false;
}
if (isDivision) {
result.setText(value1 / value2 + "");
isDivision = false;
}
break;
}
}
}
```
这样,一个简单的计算器就完成了。
阅读全文