制作一个简易计算器
两部分
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteY="244dp">
<EditText
android:id="@+id/res"
android:layout_width="match_parent"
android:layout_height="100dp"
android:editable="false"
android:inputType="number"
android:textSize="30dp"
android:text="公主请计算(先按C清除内容):">
</EditText>
<TableRow tools:ignore="MissingConstraints">
<Button
android:id="@+id/btclear"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="C" />
<Button
android:id="@+id/btpoint"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="." />
<Button
android:id="@+id/btmo"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="%" />
<Button
android:id="@+id/btchu"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="÷" />
</TableRow>
<TableRow tools:ignore="MissingConstraints">
<Button
android:id="@+id/bt7"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="7" />
<Button
android:id="@+id/bt8"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="8" />
<Button
android:id="@+id/bt9"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="9" />
<Button
android:id="@+id/btmul"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="*" />
</TableRow>
<TableRow>
<Button
android:id="@+id/bt4"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="4" />
<Button
android:id="@+id/bt5"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="5" />
<Button
android:id="@+id/bt6"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="6" />
<Button
android:id="@+id/btreduce"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="-" />
</TableRow>
<TableRow tools:ignore="MissingConstraints">
<Button
android:id="@+id/bt1"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/bt2"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="2" />
<Button
android:id="@+id/bt3"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="3" />
<Button
android:id="@+id/btadd"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="+" />
</TableRow>
<TableRow tools:ignore="MissingConstraints">
<Button
android:id="@+id/bt0"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="0" />
<Button
android:id="@+id/btequal"
android:layout_width="0dp"
android:layout_height="65dp"
android:textSize="20dp"
android:layout_weight="1"
android:text="=" />
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.op2;
import android.*;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
EditText res;
Button zero,one,two,three,four,five,six,seven,eight,nine;
Button mul,reduce,add,chu;
Button mo;
Button gen;
Button equal;
Button C;
Button point;
Boolean clr_flag=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
res=(EditText) findViewById(R.id.res);
zero=(Button) findViewById(R.id.bt0);
one=(Button) findViewById(R.id.bt1);
two=(Button) findViewById(R.id.bt2);
three=(Button) findViewById(R.id.bt3);
four=(Button) findViewById(R.id.bt4);
five=(Button) findViewById(R.id.bt5);
six=(Button) findViewById(R.id.bt6);
seven=(Button) findViewById(R.id.bt7);
eight=(Button) findViewById(R.id.bt8);
nine=(Button) findViewById(R.id.bt9);
mul=(Button) findViewById(R.id.btmul);
add=(Button) findViewById(R.id.btadd);
reduce=(Button) findViewById(R.id.btreduce);
chu=(Button) findViewById(R.id.btchu);
equal=(Button) findViewById(R.id.btequal);
point=(Button) findViewById(R.id.btpoint);
C=(Button) findViewById(R.id.btclear);
mo=(Button)findViewById(R.id.btmo);
zero.setOnClickListener((View.OnClickListener) this);
one.setOnClickListener((View.OnClickListener) this);
two.setOnClickListener((View.OnClickListener) this);
three.setOnClickListener((View.OnClickListener) this);
four.setOnClickListener((View.OnClickListener) this);
five.setOnClickListener((View.OnClickListener) this);
six.setOnClickListener((View.OnClickListener) this);
seven.setOnClickListener((View.OnClickListener) this);
eight.setOnClickListener((View.OnClickListener) this);
nine.setOnClickListener((View.OnClickListener) this);
mul.setOnClickListener((View.OnClickListener) this);
add.setOnClickListener((View.OnClickListener) this);
reduce.setOnClickListener((View.OnClickListener) this);
chu.setOnClickListener((View.OnClickListener) this);
point.setOnClickListener((View.OnClickListener) this);
equal.setOnClickListener((View.OnClickListener) this);
C.setOnClickListener((View.OnClickListener) this);
mo.setOnClickListener(this);
}
@SuppressLint("SetTextI18n")
public void onClick(View view){
String str=res.getText().toString();
switch(view.getId()){
case R.id.bt0:
case R.id.bt1:
case R.id.bt2:
case R.id.bt3:
case R.id.bt4:
case R.id.bt5:
case R.id.bt6:
case R.id.bt7:
case R.id.bt8:
case R.id.bt9:
case R.id.btpoint:
if(clr_flag)
str="";
res.setText(str+((Button)view).getText());
clr_flag=false;
break;
case R.id.btmul:
case R.id.btadd:
case R.id.btreduce:
case R.id.btchu:
case R.id.btmo:
if(clr_flag)
str="";
if(str.contains("+")||str.contains("-")||str.contains("×")||str.contains("÷")||str.contains("%"))
str=str.substring(0,str.indexOf(" "));
res.setText(str+" "+((Button)view).getText()+" ");
clr_flag=false;
break;
case R.id.btclear:
res.setText("");
str="";
break;
case R.id.btequal:
getResult();
break;
}
}
private void getResult(){
clr_flag=true;
String exp=res.getText().toString();
double cnt=0;
String []arr=exp.split(" ");
if(arr[0].equals("√")){
cnt=Math.pow(Double.parseDouble(arr[1]), 0.5);
}
double d1=Double.parseDouble(arr[0]);
double d2=Double.parseDouble(arr[2]);
if(arr[1].equals("+"))
cnt=d1+d2;
else if (arr[1].equals("-"))
cnt=d1-d2;
else if(arr[1].equals("*"))
cnt=d1*d2;
else if (arr[1].equals("÷"))
cnt=d1/d2;
else if(arr[1].equals("%"))
cnt=d1%d2;
res.setText(String.valueOf(cnt));
}
}