等号不能执行,就好像没有监听时间一样,但是会有按键音;另外如果输入3+3+3就会崩溃。
输入2个运算符号点=就会崩溃。
package com.app.app13jisuanqi;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
private Button bt0;//定义0按钮
private Button bt1;//定义1按钮
private Button bt2;//定义2按钮
private Button bt3;//定义3按钮
private Button bt4;//定义4按钮
private Button bt5;//定义5按钮
private Button bt6;//定义6按钮
private Button bt7;//定义7按钮
private Button bt8;//定义8按钮
private Button bt9;//定义9按钮
private Button btjia;//定义+按钮
private Button btjian;//定义-按钮
private Button btcheng;//定义*按钮
private Button btchu;//定义÷按钮
private Button btdel;//定义删除按钮
private Button btdeng;//定义等于按钮
private Button btc;//定义清除按钮
private Button btdian;//定义.按钮
private EditText et;
private boolean clear_Flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt0=(Button) findViewById(R.id.bt0);
bt1=(Button) findViewById(R.id.bt1);
bt2=(Button) findViewById(R.id.bt2);
bt3=(Button) findViewById(R.id.bt3);
bt4=(Button) findViewById(R.id.bt4);
bt5=(Button) findViewById(R.id.bt5);
bt6=(Button) findViewById(R.id.bt6);
bt7=(Button) findViewById(R.id.bt7);
bt8=(Button) findViewById(R.id.bt8);
bt9=(Button) findViewById(R.id.bt9);
bt0=(Button) findViewById(R.id.bt0);
btdian=(Button) findViewById(R.id.btdian);
btdeng=(Button) findViewById(R.id.btdeng);
btjia=(Button) findViewById(R.id.btjia);
btjian=(Button) findViewById(R.id.btjian);
btcheng=(Button) findViewById(R.id.btcheng);
btchu=(Button) findViewById(R.id.btchu);
btc=(Button) findViewById(R.id.btc);
btdel=(Button) findViewById(R.id.btdel);
et=(EditText) findViewById(R.id.et1);
bt0.setOnClickListener(this);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
bt5.setOnClickListener(this);
bt6.setOnClickListener(this);
bt7.setOnClickListener(this);
bt8.setOnClickListener(this);
bt9.setOnClickListener(this);
btdian.setOnClickListener(this);
btjia.setOnClickListener(this);
btjian.setOnClickListener(this);
btcheng.setOnClickListener(this);
btchu.setOnClickListener(this);
btdel.setOnClickListener(this);
btc.setOnClickListener(this);
btdeng.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str=et.getText().toString();
switch (v.getId()) {
case R.id.bt0:
case R.id.bt1:
case R.id.bt2:
case R.id.bt3:
case R.id.bt4:
case R.id.bt5:
case R.id.bt6:
case R.id.bt7:
case R.id.bt8:
case R.id.bt9:
case R.id.btdian:
if (clear_Flag) {
clear_Flag=false;
et.setText("");
}
et.setText(str+((Button)v).getText());
break;
case R.id.btjia:
case R.id.btjian:
case R.id.btcheng:
case R.id.btchu:
if (clear_Flag) {
clear_Flag=false;
et.setText("");
}
et.setText(str+" "+((Button)v).getText()+"");
break;
case R.id.btc:
clear_Flag=false;
et.setText("");
break;
case R.id.btdel:
if (clear_Flag) {
clear_Flag=false;
et.setText("");
}
else if (str!=null&&str.equals("")) {
et.setText(str.substring(0, str.length()-1));
}
break;
case R.id.btdeng:
getResult();
break;
}
}
private void getResult(){
String exp=et.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;
}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.setText(r+"");
}else{
et.setText(result+"");
}
}else if (!s1.equals("")&&s2.equals("")) {
et.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;
}
}else {
et.setText("");
}
}
}