我的计算器里等于没有反应,求大神指点
package com.lh.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener{
Button bt_0;
Button bt_1;
Button bt_2;
Button bt_3;
Button bt_4;
Button bt_5;
Button bt_6;
Button bt_7;
Button bt_8;
Button bt_9;
Button bt_clear;
Button bt_delete;
Button bt_plus;
Button bt_point;
Button bt_minus;
Button bt_multiply;
Button bt_equl;
Button bt_divide;
EditText et_input;
boolean clear_flag;//清空标识
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_0=(Button) findViewById(R.id.button_0);
bt_1=(Button) findViewById(R.id.button_1);
bt_2=(Button) findViewById(R.id.button_2);
bt_3=(Button) findViewById(R.id.button_3);
bt_4=(Button) findViewById(R.id.button_4);
bt_5=(Button) findViewById(R.id.button_5);
bt_6=(Button) findViewById(R.id.button_6);
bt_7=(Button) findViewById(R.id.button_7);
bt_8=(Button) findViewById(R.id.button_8);
bt_9=(Button) findViewById(R.id.button_9);
bt_clear=(Button) findViewById(R.id.button_clear);
bt_delete=(Button) findViewById(R.id.button_delete);
bt_divide=(Button) findViewById(R.id.button_divide);
bt_multiply=(Button) findViewById(R.id.button_multiply);
bt_minus=(Button) findViewById(R.id.button_minus);
bt_plus=(Button) findViewById(R.id.button_plus);
bt_equl=(Button) findViewById(R.id.button_equl);
bt_point=(Button) findViewById(R.id.button_point);
et_input=(EditText) findViewById(R.id.et_input);
//以上为各个控件的实例化
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_plus.setOnClickListener(this);
bt_multiply.setOnClickListener(this);
bt_divide.setOnClickListener(this);
bt_minus.setOnClickListener(this);
bt_point.setOnClickListener(this);
//以上是设置按钮的点击事件
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str=et_input.getText().toString();
switch(v.getId()){
case R.id.button_0 :
if(clear_flag){
clear_flag=false;
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.button_1:
if(clear_flag){
clear_flag=false;
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.button_2:
if(clear_flag){
clear_flag=false;
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.button_3:
if(clear_flag){
clear_flag=false;
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.button_4:
if(clear_flag){
clear_flag=false;
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.button_5:
if(clear_flag){
clear_flag=false;
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.button_6:
if(clear_flag){
clear_flag=false;
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.button_7:
if(clear_flag){
clear_flag=false;
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.button_8:
if(clear_flag){
clear_flag=false;
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.button_9:
if(clear_flag){
clear_flag=false;
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.button_point:
if(clear_flag){
clear_flag=false;
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.button_plus:
if(clear_flag){
clear_flag=false;
et_input.setText("");
}
et_input.setText(str+" "+((Button)v).getText()+" ");
break;
case R.id.button_minus:
if(clear_flag){
clear_flag=false;
et_input.setText("");
}
et_input.setText(str+" "+((Button)v).getText()+" ");
break;
case R.id.button_multiply:
if(clear_flag){
clear_flag=false;
et_input.setText("");
}
et_input.setText(str+" "+((Button)v).getText()+" ");
break;
case R.id.button_divide:
if(clear_flag){
clear_flag=false;
et_input.setText("");
}
et_input.setText(str+" "+((Button)v).getText()+" ");
break;
case R.id.button_equl://等于
{if (str==null||str.equals("")){
return;
}
if (!str.contains("")){
return;
}
if(clear_flag){
clear_flag=false;
return;
}
clear_flag=true;
double result=0;
String sarray[]=str.split("+|-|×|÷",3);
String s1=sarray[0];
String op=sarray[1];
String s2=sarray[2];
double d1=Double.parseDouble(s1);
double d2=Double.parseDouble(s2);
if(!s1.equals("")&&!s2.equals("")){
if(op.equals("+")){
result=d1+d2;
}
else if(op.equals("-")){
result=d1-d2;
}
else if(op.equals("×")){
result=d1*d2;
}
else if(op.equals("÷")){
if(d2==0){
result=0;
}else{
result=d1/d2;
}
}
if(!s1.contains(".")&&!s2.contains(".")){
int r=(int)result;
et_input.setText(r+"");
}else{
et_input.setText(result+"");
}
}
else if(!s1.equals("")&&s2.equals("")){
et_input.setText(str);
}
else if(s1.equals("")&&!s2.equals("")){
if(op.equals("+")){
result=0+d2;
}
else if(op.equals("-")){
result=0-d2;
}
else if(op.equals("×")){
result=0*d2;
}
else if(op.equals("÷")){
result=0;
}
if(!s2.contains(".")){
int r=(int)result;
et_input.setText(r+"");
}else{
et_input.setText(result+"");
}
}
else{
et_input.setText("");
}
}
break;
case R.id.button_clear:
et_input.setText("");
break;
case R.id.button_delete:
if( !str.equals("")&&!(str==null)){
et_input.setText(str.substring(0, str.length()-1));
break;
}
}
}
}