运算符+ - *都没有操作反应只有÷正常?
@Override
public void onClick(View v) {
String str=et_input.getText().toString();
switch(v.getId()){
case R.id.btn_0:
case R.id.btn_1:
case R.id.btn_2:
case R.id.btn_3:
case R.id.btn_4:
case R.id.btn_5:
case R.id.btn_6:
case R.id.btn_7:
case R.id.btn_8:
case R.id.btn_9:
case R.id.btn_point:
if(needclear){
needclear=false;
str=" ";
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.btn_plus:
case R.id.btn_minus:
case R.id.btn_multiply:
case R.id.btn_divide:
if(needclear){
needclear=false;
str=" ";
et_input.setText("");
return;
}
et_input.setText(str+" "+((Button)v).getText()+" ");
break;
case R.id.btn_del:
if(needclear){
needclear=false;
str=" ";
et_input.setText("");
}else if(str!=null&&!str.equals("")){
et_input.setText(str.substring(0, str.length()-1));
}
break;
case R.id.btn_clear:
needclear=false;
str=" ";
et_input.setText("");
case R.id.btn_equal:
getResult();
break;
}
}
/*
* 运算结果
*/
private void getResult() {
// TODO Auto-generated method stub
String exp=et_input.getText().toString();
if(exp==null||exp.equals("")){
return;
}if(!exp.contains(" ")){
return;
}
double r=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 arg1=Double.parseDouble(s1);
double arg2=Double.parseDouble(s2);
if(op.equals("+")){
r=arg1+arg2;
}else if(op.equals("-")){
r=arg1-arg2;
}else if(op.equals("×")){
r=arg1*arg2;
}else if(op.equals("÷")){
if(arg2==0){
r=0;
}else{
r=arg1/arg2;
}
if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")){
int result=(int)r;
et_input.setText(result+"");
}else{
et_input.setText(r+"");
}
}else if(!s1.equals("")&&s2.equals("")){
et_input.setText(exp);
}else if(s1.equals("")&&!s2.equals("")){
et_input.setText(r+"");
if(op.equals("+")){
r=0+arg2;
}else if(op.equals("-")){
r=0-arg2;
}else if(op.equals("×")){
r=0;
}else if(op.equals("÷")){
r=0;
}
if(!s2.contains(".")){
int result=(int)r;
et_input.setText(result+"");
}else {
et_input.setText(r+"");
}
} else {
et_input.setText("");
}
}}
}