android studio计算器代码
时间: 2023-09-13 14:09:59 浏览: 103
android studio计算器实现代码
以下是一个简单的Android Studio计算器代码:
MainActivity.java
```java
package com.example.calculator;
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 num1, num2;
Button add, subtract, multiply, divide;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = findViewById(R.id.num1);
num2 = findViewById(R.id.num2);
add = findViewById(R.id.add);
subtract = findViewById(R.id.subtract);
multiply = findViewById(R.id.multiply);
divide = findViewById(R.id.divide);
result = findViewById(R.id.result);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double n1 = Double.parseDouble(num1.getText().toString());
double n2 = Double.parseDouble(num2.getText().toString());
double res = n1 + n2;
result.setText(String.valueOf(res));
}
});
subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double n1 = Double.parseDouble(num1.getText().toString());
double n2 = Double.parseDouble(num2.getText().toString());
double res = n1 - n2;
result.setText(String.valueOf(res));
}
});
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double n1 = Double.parseDouble(num1.getText().toString());
double n2 = Double.parseDouble(num2.getText().toString());
double res = n1 * n2;
result.setText(String.valueOf(res));
}
});
divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double n1 = Double.parseDouble(num1.getText().toString());
double n2 = Double.parseDouble(num2.getText().toString());
double res = n1 / n2;
result.setText(String.valueOf(res));
}
});
}
}
```
activity_main.xml
```xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<EditText
android:id="@+id/num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="Enter First Number"
android:inputType="numberDecimal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/num1"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Enter Second Number"
android:inputType="numberDecimal"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/num2"
android:layout_marginTop="20dp"
android:text="Add"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/subtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/add"
android:layout_marginTop="20dp"
android:text="Subtract"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/subtract"
android:layout_marginTop="20dp"
android:text="Multiply"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/multiply"
android:layout_marginTop="20dp"
android:text="Divide"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/divide"
android:layout_marginTop="20dp"
android:text="Result"
android:textSize="24sp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
```
这个计算器只支持加减乘除四个基本运算。您可以根据需要扩展它。
阅读全文