等号 点击没反应,请解答谢谢!
package com.example.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements android.view.View.OnClickListener{
Button btn_0;//0按钮
Button btn_1;//1按钮
Button btn_2;//2按钮
Button btn_3;//3按钮
Button btn_4;//4按钮
Button btn_5;//5按钮
Button btn_6;//6按钮
Button btn_7;//7按钮
Button btn_8;//8按钮
Button btn_9;//9按钮
Button btn_point;//.按钮
Button btn_cleaner;//清除按钮
Button btn_del;//删除按钮
Button btn_jia;
Button btn_jian;
Button btn_cheng;
Button btn_chu;
Button btn_dengyu;
EditText et_input;
boolean clear_flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_0=(Button) findViewById(R.id.ling);
btn_1=(Button) findViewById(R.id.yi);
btn_2=(Button) findViewById(R.id.er);
btn_3=(Button) findViewById(R.id.san);
btn_4=(Button) findViewById(R.id.si);
btn_5=(Button) findViewById(R.id.wu);
btn_6=(Button) findViewById(R.id.liu);
btn_7=(Button) findViewById(R.id.seven);
btn_8=(Button) findViewById(R.id.eight);
btn_9=(Button) findViewById(R.id.night);
btn_point=(Button) findViewById(R.id.dian);
btn_cleaner=(Button) findViewById(R.id.cleaner);
btn_del=(Button) findViewById(R.id.del);
btn_jia=(Button) findViewById(R.id.jiahao);
btn_jian=(Button) findViewById(R.id.jianhao);
btn_cheng=(Button) findViewById(R.id.chenghao);
btn_chu=(Button) findViewById(R.id.chuhao);
btn_dengyu=(Button) findViewById(R.id.dengyu);
et_input=(EditText) findViewById(R.id.et_input);
btn_0.setOnClickListener(this);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
btn_4.setOnClickListener(this);
btn_5.setOnClickListener(this);
btn_6.setOnClickListener(this);
btn_7.setOnClickListener(this);
btn_8.setOnClickListener(this);
btn_9.setOnClickListener(this);
btn_point.setOnClickListener(this);
btn_cleaner.setOnClickListener(this);
btn_del.setOnClickListener(this);
btn_jia.setOnClickListener(this);
btn_jian.setOnClickListener(this);
btn_cheng.setOnClickListener(this);
btn_chu.setOnClickListener(this);
btn_dengyu.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String str = et_input.getText().toString();
switch (v.getId()) {
case R.id.ling:
case R.id.yi:
case R.id.er:
case R.id.san:
case R.id.si:
case R.id.wu:
case R.id.liu:
case R.id.seven:
case R.id.eight:
case R.id.night:
case R.id.dian:
if(clear_flag){
clear_flag = false;
str ="";
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.jiahao:
case R.id.jianhao:
case R.id.chenghao:
case R.id.chuhao:
if(clear_flag){
clear_flag = false;
et_input.setText("");
}
et_input.setText(str+" "+((Button)v).getText()+" ");
break;
case R.id.del:
if(clear_flag){
clear_flag = false;
et_input.setText("");
}else if(str!=null&&!str.equals("")){
et_input.setText(str.subSequence(0, str.length()-1));
}
break;
case R.id.cleaner:
clear_flag = false;
et_input.setText("");
break;
case R.id.dengyu:
break;
}
}
private void getResult(){
String exp = et_input.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);
double d1 =Double.parseDouble(s1);
double d2 =Double.parseDouble(s2);
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( exp);
}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;
}else if(op.equals("÷")){
if(d2==0){
result =0;
}else{
result =0;
}
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("");
}
}
}
}