安卓studio计算器代码
时间: 2023-06-21 15:19:47 浏览: 41
以下是一个简单的安卓Studio计算器代码示例,可以实现基本的加、减、乘、除运算:
activity_main.xml:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入数字"
android:inputType="numberDecimal"
android:textAlignment="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"/>
<Button
android:id="@+id/btn_subtract"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"/>
<Button
android:id="@+id/btn_multiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"/>
<Button
android:id="@+id/btn_divide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"/>
</LinearLayout>
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"/>
</LinearLayout>
```
MainActivity.java:
```java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText etInput;
private Button btnAdd, btnSubtract, btnMultiply, btnDivide;
private TextView tvResult;
private float number1 = 0, number2 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etInput = findViewById(R.id.et_input);
btnAdd = findViewById(R.id.btn_add);
btnSubtract = findViewById(R.id.btn_subtract);
btnMultiply = findViewById(R.id.btn_multiply);
btnDivide = findViewById(R.id.btn_divide);
tvResult = findViewById(R.id.tv_result);
btnAdd.setOnClickListener(this);
btnSubtract.setOnClickListener(this);
btnMultiply.setOnClickListener(this);
btnDivide.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String input = etInput.getText().toString();
if (input.equals("")) {
return;
}
if (number1 == 0) {
number1 = Float.parseFloat(input);
etInput.setText("");
} else {
number2 = Float.parseFloat(input);
etInput.setText(String.valueOf(calculate()));
number1 = 0;
number2 = 0;
}
switch (v.getId()) {
case R.id.btn_add:
etInput.setText("");
break;
case R.id.btn_subtract:
etInput.setText("");
break;
case R.id.btn_multiply:
etInput.setText("");
break;
case R.id.btn_divide:
etInput.setText("");
break;
}
}
private float calculate() {
if (btnAdd.isPressed()) {
return number1 + number2;
} else if (btnSubtract.isPressed()) {
return number1 - number2;
} else if (btnMultiply.isPressed()) {
return number1 * number2;
} else if (btnDivide.isPressed()) {
if (number2 == 0) {
return Float.NaN;
} else {
return number1 / number2;
}
}
return 0;
}
}
```
这个示例中,用户输入一个数字后,点击加、减、乘、除按钮,可以计算出两个数字的结果,并在TextView中显示。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)