Android studio实现计算器
calculate.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:orientation="horizontal">
<EditText
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="310dp"
android:hint="请输入计算式"
android:textSize="30dp"></EditText>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TableRow
android:orientation="horizontal"
>
<Button
android:id="@+id/clear"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="C"
>
</Button>
<Button
android:id="@+id/del"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="del"
>
</Button>
<Button
android:id="@+id/add"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="+"
>
</Button>
<Button
android:id="@+id/sub"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="-"
>
</Button>
</TableRow>
<TableRow
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_7"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="7"
>
</Button>
<Button
android:id="@+id/btn_8"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="8"
>
</Button>
<Button
android:id="@+id/btn_9"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="9"
>
</Button>
<Button
android:id="@+id/mul"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="X"
>
</Button>
</TableRow>
<TableRow
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_4"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="4"
>
</Button>
<Button
android:id="@+id/btn_5"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="5"
>
</Button>
<Button
android:id="@+id/btn_6"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="6"
>
</Button>
<Button
android:id="@+id/div"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="/"
>
</Button>
</TableRow>
<TableRow
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_1"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="1"
>
</Button>
<Button
android:id="@+id/btn_2"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="2"
>
</Button>
<Button
android:id="@+id/btn_3"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="3"
>
</Button>
<Button
android:id="@+id/btn_point"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="."
>
</Button>
</TableRow>
<TableRow
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_0"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="0"
>
</Button>
<Button
android:id="@+id/btn_eq"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="="
>
</Button>
</TableRow>
</TableLayout>
</LinearLayout>
Mainactivity.java
package com.example.calaulate;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;
Button btn6;
Button btn7;
Button btn8;
Button btn9;
Button btn0;
Button add;
Button sub;
Button mul;
Button div;
Button dot;
Button equ;
Button clear;
EditText result;
double num1 = 0, num2 = 0;
double Result = 0;
int op = 0;
String opd = " ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculate);
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);
add = findViewById(R.id.add);
sub = findViewById(R.id.sub);
mul = findViewById(R.id.mul);
div = findViewById(R.id.div);
equ = findViewById(R.id.btn_eq);
dot = findViewById(R.id.btn_point);
clear = findViewById(R.id.clear);
result = findViewById(R.id.result);
btn1.setOnClickListener(new Click());
btn2.setOnClickListener(new Click());
btn3.setOnClickListener(new Click());
btn4.setOnClickListener(new Click());
btn5.setOnClickListener(new Click());
btn6.setOnClickListener(new Click());
btn7.setOnClickListener(new Click());
btn8.setOnClickListener(new Click());
btn9.setOnClickListener(new Click());
btn0.setOnClickListener(new Click());
add.setOnClickListener(new Click());
sub.setOnClickListener(new Click());
mul.setOnClickListener(new Click());
div.setOnClickListener(new Click());
equ.setOnClickListener(new Click());
dot.setOnClickListener(new Click());
clear.setOnClickListener(new Click());
result.setOnClickListener(new Click());
}
class Click implements View.OnClickListener {
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_0:
String myString = result.getText().toString();
myString += "0";
result.setText(myString);
break;
case R.id.btn_1:
String myString1 = result.getText().toString();
myString1 += "1";
result.setText(myString1);
break;
case R.id.btn_2:
String myString2 = result.getText().toString();
myString2 += "2";
result.setText(myString2);
break;
case R.id.btn_3:
String myString3 = result.getText().toString();
myString3 += "3";
result.setText(myString3);
break;
case R.id.btn_4:
String myString4 = result.getText().toString();
myString4 += "4";
result.setText(myString4);
break;
case R.id.btn_5:
String myString5 = result.getText().toString();
myString5 += "5";
result.setText(myString5);
break;
case R.id.btn_6:
String myString6 = result.getText().toString();
myString6 += "6";
result.setText(myString6);
break;
case R.id.btn_7:
String myString7 = result.getText().toString();
myString7 += "7";
result.setText(myString7);
break;
case R.id.btn_8:
String myString8 = result.getText().toString();
myString8 += "8";
result.setText(myString8);
break;
case R.id.btn_9:
String myString9 = result.getText().toString();
myString9 += "9";
result.setText(myString9);
break;
case R.id.btn_point:
String myStringDot = result.getText().toString();
myStringDot += ".";
result.setText(myStringDot);
break;
case R.id.add:
String myStringAdd = result.getText().toString();
if (myStringAdd.equals(null)) {
return;
}
num1 = Double.valueOf(myStringAdd);
result.setText(null);
op = 1;
opd = "+";
break;
case R.id.sub:
String myStringSub = result.getText().toString();
if (myStringSub.equals(null)) {
return;
}
num1 = Double.valueOf(myStringSub);
result.setText(null);
op = 2;
opd = "-";
break;
case R.id.mul:
String myStringMul = result.getText().toString();
if (myStringMul.equals(null)) {
return;
}
num1 = Double.valueOf(myStringMul);
result.setText(null);
op = 3;
opd = "×";
break;
case R.id.div:
String myStringDiv = result.getText().toString();
if (myStringDiv.equals(null)) {
return;
}
num1 = Double.valueOf(myStringDiv);
result.setText(null);
op = 4;
opd = "÷";
break;
case R.id.clear:
result.setText(null);
break;
case R.id.btn_eq:
String myStringEqu = result.getText().toString();
if (myStringEqu.equals(null)) {
return;
}
num2 = Double.valueOf(myStringEqu);
result.setText(null);
switch (op) {
case 0:
Result = num2;
break;
case 1:
Result = num1 + num2;
break;
case 2:
Result = num1 - num2;
break;
case 3:
Result = num1 * num2;
break;
case 4:
if (num2 == 0)
Result = 0;
else
Result = num1 / num2;
break;
default:
Result = 0;
break;
}
result.setText(Double.toString(num1) + opd + Double.toString(num2) + "=" + Double.toString(Result));
break;
default:
break;
}
}
}
}
|