Android Studio 计算器代码
时间: 2023-11-02 13:08:48 浏览: 122
android studio计算器实现代码
以下是一个基本的 Android Studio 计算器代码,可以进行加减乘除运算:
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 {
EditText etNumber1, etNumber2;
Button btnAdd, btnSubtract, btnMultiply, btnDivide;
TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etNumber1 = findViewById(R.id.etNumber1);
etNumber2 = findViewById(R.id.etNumber2);
btnAdd = findViewById(R.id.btnAdd);
btnSubtract = findViewById(R.id.btnSubtract);
btnMultiply = findViewById(R.id.btnMultiply);
btnDivide = findViewById(R.id.btnDivide);
tvResult = findViewById(R.id.tvResult);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double result = Double.parseDouble(etNumber1.getText().toString()) + Double.parseDouble(etNumber2.getText().toString());
tvResult.setText(Double.toString(result));
}
});
btnSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double result = Double.parseDouble(etNumber1.getText().toString()) - Double.parseDouble(etNumber2.getText().toString());
tvResult.setText(Double.toString(result));
}
});
btnMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double result = Double.parseDouble(etNumber1.getText().toString()) * Double.parseDouble(etNumber2.getText().toString());
tvResult.setText(Double.toString(result));
}
});
btnDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double result = Double.parseDouble(etNumber1.getText().toString()) / Double.parseDouble(etNumber2.getText().toString());
tvResult.setText(Double.toString(result));
}
});
}
}
```
activity_main.xml 布局文件:
```xml
<?xml version="1.0" encoding="utf-8"?>
<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/etNumber1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter number 1" />
<EditText
android:id="@+id/etNumber2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter number 2" />
<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add" />
<Button
android:id="@+id/btnSubtract"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtract" />
<Button
android:id="@+id/btnMultiply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Multiply" />
<Button
android:id="@+id/btnDivide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Divide" />
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result" />
</LinearLayout>
```
该代码使用了四个按钮(加、减、乘、除)来计算两个输入数的结果,并在 TextView 中显示结果。注意需要在 AndroidManifest.xml 文件中添加适当的权限和配置。
阅读全文