xml样式
java代码
package com.example.myapplication4;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button button4,button5,button6,button8,button9,button10,
button12,button13,button14,button17;
private TextView tv_show;
private Button button_qinchu,button_shanchu,button_chu,button_jia,button_jian,button_cheng,
button_dian,button_den;
private boolean isChar=false;
private StringBuffer strA=new StringBuffer(),strB=new StringBuffer();
char operator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init(){
button_qinchu=this.findViewById(R.id.button1);
button_shanchu=this.findViewById(R.id.button2);
button_chu=this.findViewById(R.id.button3);
button4=this.findViewById(R.id.button4);
button5=this.findViewById(R.id.button5);
button6=this.findViewById(R.id.button6);
button_cheng=this.findViewById(R.id.button7);
button8=this.findViewById(R.id.button8);
button9=this.findViewById(R.id.button9);
button10=this.findViewById(R.id.button10);
button_jia=this.findViewById(R.id.button11);
button12=this.findViewById(R.id.button12);
button13=this.findViewById(R.id.button13);
button14=this.findViewById(R.id.button14);
button_jian=this.findViewById(R.id.button15);
button_dian=this.findViewById(R.id.button16);
button17=this.findViewById(R.id.button17);
button_den=this.findViewById(R.id.button18);
tv_show=this.findViewById(R.id.tv_show);
button_qinchu.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
if (isChar==false){
strA=new StringBuffer();
}else {
strB=new StringBuffer();
}
tv_show.setText("0");
}
});
button_shanchu.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
if (isChar==false) {
if(strA.length()==0){
tv_show.setText("0");
}else{
strA.deleteCharAt(strA.length()-1);
tv_show.setText(strA.toString());}
if(strA.length()==0){
tv_show.setText("0");
}
}
else{
if(strB.length()==0){
tv_show.setText("0");
}
else{
strB.deleteCharAt(strB.length()-1);
tv_show.setText(strB.toString());}
if(strB.length()==0){
tv_show.setText("0");
}
}
}
});
button_jia.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
tv_show.setText("+");
isChar=true;
operator=0;
}
});
button_jian.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
tv_show.setText("-");
operator = 0;
isChar=true;
operator=1;
}
});
button_cheng.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
tv_show.setText("乘");
operator = 0;
isChar=true;
operator=2;
}
});
button_chu.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
tv_show.setText("除");
operator = 0;
isChar=true;
operator=3;
}
});
button_den.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
if (strA.length()==0&&strB.length()==0){
strA.append(0);
strB.append(0);
}else if(strA.length()==0){
strA.append(0);
}else if (strB.length()==0){
strB.append(0);
}
double one=Double.parseDouble(strA.toString());
double two=Double.parseDouble(strB.toString());
double sum=0;
switch (operator){
case 0:
isChar=false;
sum=one+two;
break;
case 1:
isChar=false;
sum=one-two;
break;
case 2:
isChar=false;
sum=one*two;
break;
case 3:
isChar=false;
sum=one/two;
break;
}
tv_show.setText(sum+"");
strA = new StringBuffer();
strB = new StringBuffer();
// 表示从第二轮计算开始,又是从第一个(digA)数开始赋值
isChar = false;
}
});
}
public void MyonClick(View v) {
if (isChar==false){switch (v.getId()) {
case R.id.button4:
strA.append(7);
tv_show.setText(strA);
break;
case R.id.button5:
strA.append(8);
tv_show.setText(strA);
break;
case R.id.button6:
strA.append(9);
tv_show.setText(strA);
break;
case R.id.button8:
strA.append(4);
tv_show.setText(strA);
break;
case R.id.button9:
strA.append(5);
tv_show.setText(strA);
break;
case R.id.button10:
strA.append(6);
tv_show.setText(strA);
break;
case R.id.button12:
strA.append(1);
tv_show.setText(strA);
break;
case R.id.button13:
strA.append(2);
tv_show.setText(strA);
break;
case R.id.button14:
strA.append(3);
tv_show.setText(strA);
break;
case R.id.button16:
strA.append(".");
tv_show.setText(strA);
break;
case R.id.button17:
strA.append(0);
tv_show.setText(strA);
break;
}}else {switch (v.getId()) {
case R.id.button4:
strB.append(7);
tv_show.setText(strB);
break;
case R.id.button5:
strB.append(8);
tv_show.setText(strB);
break;
case R.id.button6:
strB.append(9);
tv_show.setText(strB);
break;
case R.id.button8:
strB.append(4);
tv_show.setText(strB);
break;
case R.id.button9:
strB.append(5);
tv_show.setText(strB);
break;
case R.id.button10:
strB.append(6);
tv_show.setText(strB);
break;
case R.id.button12:
strB.append(1);
tv_show.setText(strB);
break;
case R.id.button13:
strB.append(2);
tv_show.setText(strB);
break;
case R.id.button14:
strB.append(3);
tv_show.setText(strB);
break;
case R.id.button16:
strA.append(".");
tv_show.setText(strB);
break;
case R.id.button17:
strB.append(0);
tv_show.setText(strB);
break;
}}
}
}