public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button Bt_0;
private Button Bt_1;
private Button Bt_2;
private Button Bt_3;
private Button Bt_4;
private Button Bt_5;
private Button Bt_6;
private Button Bt_7;
private Button Bt_8;
private Button Bt_9;
private Button Bt_clear;
private Button Bt_delete;
private Button Bt_divide;
private Button Bt_multiply;
private Button Bt_minus;
private Button Bt_add;
private Button Bt_equal;
private Button Bt_point;
private EditText Et1;
private boolean clear_flag;//清空标识
protected static final String tag="calculater";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bt_0= (Button) findViewById(R.id.Bt_0);
Bt_1= (Button) findViewById(R.id.Bt_1);
Bt_2= (Button) findViewById(R.id.Bt_2);
Bt_3= (Button) findViewById(R.id.Bt_3);
Bt_4= (Button) findViewById(R.id.Bt_4);
Bt_5= (Button) findViewById(R.id.Bt_5);
Bt_6= (Button) findViewById(R.id.Bt_6);
Bt_7= (Button) findViewById(R.id.Bt_7);
Bt_8= (Button) findViewById(R.id.Bt_8);
Bt_9= (Button) findViewById(R.id.Bt_9);
Bt_clear= (Button) findViewById(R.id.Bt_clear);
Bt_divide= (Button) findViewById(R.id.Bt_divide);
Bt_delete= (Button) findViewById(R.id.Bt_delete);
Bt_multiply= (Button) findViewById(R.id.Bt_multiply);
Bt_minus= (Button) findViewById(R.id.Bt_minus);
Bt_add= (Button) findViewById(R.id.Bt_add);
Bt_equal= (Button) findViewById(R.id.Bt_equal);
Bt_point= (Button) findViewById(R.id.Bt_point);
Et1= (EditText) findViewById(R.id.Et1);
Bt_0.setOnClickListener(this);
Bt_1.setOnClickListener(this);
Bt_2.setOnClickListener(this);
Bt_3.setOnClickListener(this);
Bt_4.setOnClickListener(this);
Bt_5.setOnClickListener(this);
Bt_6.setOnClickListener(this);
Bt_7.setOnClickListener(this);
Bt_8.setOnClickListener(this);
Bt_9.setOnClickListener(this);
Bt_clear.setOnClickListener(this);
Bt_delete.setOnClickListener(this);
Bt_divide.setOnClickListener(this);
Bt_minus.setOnClickListener(this);
Bt_multiply.setOnClickListener(this);
Bt_add.setOnClickListener(this);
Bt_equal.setOnClickListener(this);
Bt_point.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String str = Et1.getText().toString();
switch (view.getId()){
case R.id.Bt_0:
case R.id.Bt_1:
case R.id.Bt_2:
case R.id.Bt_3:
case R.id.Bt_4:
case R.id.Bt_5:
case R.id.Bt_6:
case R.id.Bt_7:
case R.id.Bt_8:
case R.id.Bt_9:
case R.id.Bt_point:
if (clear_flag){
clear_flag=false;
str="";
Et1.setText("");
}
Et1.setText(str+((Button)view).getText());
break;
case R.id.Bt_add:
case R.id.Bt_minus:
case R.id.Bt_multiply:
case R.id.Bt_divide:
if (clear_flag){
clear_flag=false;
str="";
Et1.setText("");
}
Et1.setText(str+" "+((Button)view).getText()+" ");
break;
case R.id.Bt_clear:
clear_flag=false;
str="";
Et1.setText("");
break;
case R.id.Bt_delete:
if (clear_flag){
clear_flag=false;
str="";
Et1.setText("");
} else if(str!=null&&!str.equals("")){
Et1.setText(str.substring(0,str.length()-1));
}
break;
case R.id.Bt_equal:
getResult();
break;
}
}
private void getResult(){
String exp=Et1.getText().toString();
if(exp==null||exp.equals("")){
return;
}
if (!exp.contains(" ")){
return;
}if (clear_flag){
clear_flag=false;
return;
}
clear_flag=true;
double result=0;
String s1=exp.substring(0,exp.indexOf(" "));
String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);
String s2=exp.substring(exp.indexOf(" ")+3);
if(!s1.equals("")&&!s2.equals("")){
double d1=Double.parseDouble(s1);
double d2=Double.parseDouble(s2);
if(op.equals("+")){
result=d1+d2;
}else if (op.equals("-")){
result=d1-d2;
Log.i(tag,"result = "+result);
}else if (op.equals("*")){
result=d1*d2;
} else if (op.equals("÷")){
if (d2==0){
result=0;
}else {
result=d1/d2;
Log.i(tag,"result = "+result);
}
}
if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")){
int r=(int)result;
Et1.setText(r+"");
}else {
Et1.setText(result+"");
}
}else if (!s1.equals("")&&s2.equals("")){
Et1.setText(exp);
}else if (s1.equals("")&&!s2.equals("")){
double d2=Double.parseDouble(s2);
if(op.equals("+")){
result=0+d2;
}else if (op.equals("-")){
result=0-d2;
}else if (op.equals("*")){
result=0;
} else if (op.equals("/")){
result=0;
}
if(!s2.contains(".")){
int r=(int)result;
Et1.setText(r+"");
}else {
Et1.setText(result+"");
}
}
else {
Et1.setText("");
}
}
}