计算器的等于没反应
为什么计算器的等于没反应?
package com.example.mach;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
//import android.view.Window;
public class MainActivity extends ActionBarActivity implements OnClickListener{
private EditText et;
private Button bt_c;
private Button bt_del;
private Button bt_cheng;
private Button bt_minus;
private Button bt_plus;
private Button bt_equal;
private Button bt_chu;
private Button bt_point;
private Button bt_7;
private Button bt_8;
private Button bt_9;
private Button bt_4;
private Button bt_5;
private Button bt_6;
private Button bt_1;
private Button bt_2;
private Button bt_3;
private Button bt_0;
private boolean clear_flag;//清空标识
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.et1);
bt_c=(Button) findViewById(R.id.button_c);
bt_equal=(Button) findViewById(R.id.btn_equal);
bt_del=(Button) findViewById(R.id.button_del);
bt_chu=(Button) findViewById(R.id.button_chu);
bt_plus=(Button) findViewById(R.id.button_plus);
bt_minus=(Button) findViewById(R.id.button_minus);
bt_point=(Button) findViewById(R.id.button_point);
bt_cheng=(Button) findViewById(R.id.button_cheng);
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_4=(Button) findViewById(R.id.button_4);
bt_5=(Button) findViewById(R.id.button_5);
bt_6=(Button) findViewById(R.id.button_6);
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_0=(Button) findViewById(R.id.btn_0);
//设置按钮的点击事件
bt_c.setOnClickListener(this);
bt_del.setOnClickListener(this);
bt_chu.setOnClickListener(this);
bt_plus.setOnClickListener(this);
bt_cheng.setOnClickListener(this);
bt_point.setOnClickListener(this);
bt_minus.setOnClickListener(this);
bt_equal.setOnClickListener(this);
bt_7.setOnClickListener(this);
bt_8.setOnClickListener(this);
bt_9.setOnClickListener(this);
bt_4.setOnClickListener(this);
bt_5.setOnClickListener(this);
bt_6.setOnClickListener(this);
bt_1.setOnClickListener(this);
bt_2.setOnClickListener(this);
bt_3.setOnClickListener(this);
bt_0.setOnClickListener(this);
// this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View arg0) {
String str = et.getText().toString();
switch (arg0.getId()) {
case R.id.btn_0:
case R.id.button_1:
case R.id.button_2:
case R.id.button_3:
case R.id.button_4:
case R.id.button_5:
case R.id.button_6:
case R.id.button_7:
case R.id.button_8:
case R.id.button_9:
case R.id.button_point:
if(clear_flag){
clear_flag=false;
str="";
et.setText("");
}
et.setText(str+((Button) arg0).getText());
break;
case R.id.button_plus:
case R.id.button_minus:
case R.id.button_cheng:
case R.id.button_chu:
if(clear_flag){
clear_flag=false;
str="";
et.setText("");
}
et.setText(str+" "+((Button) arg0).getText()+" ");
break;
case R.id.button_del:
if(clear_flag){
clear_flag=false;
str="";
et.setText("");
}
else if(str!=null&&str.equals("")){
et.setText(str.substring(0, str.length()-1));
}
break;
case R.id.button_c:
clear_flag=false;
str="";
et.setText("");
break;
case R.id.btn_equal:
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){
Toast.makeText(MainActivity.this, "除数不为0!", Toast.LENGTH_LONG).show();
et.setText("0");
}
else{
result=d1/d2;
}
}
if(!s1.contains(".")&&!s2.contains(".")&&!op.contains("+")){
int r= (int) result;
et.setText(r+"");
}else {
et.setText(result+"");
}
}
else if(!s1.equals("")&&s2.equals("")){
double d1=Double.parseDouble(s1);
result=d1;
Toast.makeText(MainActivity.this, "不具备运算!", Toast.LENGTH_LONG).show();
et.setText(result+"");
}
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;
et.setText(r+"");
}else {
et.setText(result+"");
}
}else{
et.setText("");
}
}
}