照着课程慢慢做出来的计算器,但是最后还是有问题,del和c按钮 点起来没有反应,但是就是找不出问题在哪~~求指点~~代码如下: package com.example.calculaterdemo;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{ Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_jia,btn_jian,btn_cheng,btn_chu,btn_dian,btn_sum; EditText et_input; boolean clear_flag;//清空标识 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_input = (EditText)findViewById(R.id.et_input); btn_0 = (Button) findViewById(R.id.btn_0); btn_1 = (Button) findViewById(R.id.btn_1); btn_2 = (Button) findViewById(R.id.btn_2); btn_3 = (Button) findViewById(R.id.btn_3); btn_4 = (Button) findViewById(R.id.btn_4); btn_5 = (Button) findViewById(R.id.btn_5); btn_6 = (Button) findViewById(R.id.btn_6); btn_7 = (Button) findViewById(R.id.btn_7); btn_8 = (Button) findViewById(R.id.btn_8); btn_9 = (Button) findViewById(R.id.btn_9); btn_jia = (Button) findViewById(R.id.btn_jia); btn_jian = (Button) findViewById(R.id.btn_jian); btn_cheng = (Button) findViewById(R.id.btn_cheng); btn_chu = (Button) findViewById(R.id.btn_chu); btn_dian = (Button) findViewById(R.id.btn_dian); btn_sum = (Button) findViewById(R.id.btn_sum); 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_jia.setOnClickListener(this); btn_jian.setOnClickListener(this); btn_cheng.setOnClickListener(this); btn_chu.setOnClickListener(this); btn_dian.setOnClickListener(this); btn_sum.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.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_dian: if(clear_flag){ clear_flag = false; str =""; et_input.setText(""); } et_input.setText(str+((Button)v).getText());//赋值 break; case R.id.btn_jia: case R.id.btn_jian: case R.id.btn_cheng: case R.id.btn_chu: if(clear_flag){ clear_flag = false; str =""; et_input.setText(""); } et_input.setText(str+" "+((Button)v).getText()+" "); break; case R.id.btn_c: clear_flag = false; str =""; et_input.setText(""); break; case R.id.btn_del: if(clear_flag){ clear_flag = false; str =""; et_input.setText(""); }else if(str!=null&&!str.equals("")){ et_input.setText(str.substring(0,str.length()-1));//把显示框中的内容截取,然后返回的内容为内容长度-1(str.length()-1) } break; case R.id.btn_sum: getResult(); break; default: break; } } //返回结果 public 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);//运算符后面的字符 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(".")&&!op.equals("÷")){ 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("")){ Double d2 = Double.parseDouble(s2); 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(""); } }}
4 回答
1697709906
TA贡献3条经验 获得超1个赞
del和clear两个按钮没有进行实例化。
btn_del = (Button) findViewById(R.id.btn_del);
btn_clear = (Button) findViewById(R.id.btn_clear);
lynhao
TA贡献81条经验 获得超57个赞
跟着老师的代码还是有bug的,这个是我自己修改后的
Button btn_0; Button btn_1; Button btn_2; Button btn_3; Button btn_4; Button btn_5; Button btn_6; Button btn_7; Button btn_8; Button btn_9; Button btn_point; Button btn_clear; Button btn_del; Button btn_plus; Button btn_minus; Button btn_multiply; Button btn_divide; Button btn_equal; EditText et_input; Boolean clear_flag=true; //清空标识 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_0 = (Button) findViewById(R.id.btn_0); btn_1 = (Button) findViewById(R.id.btn_1); btn_2 = (Button) findViewById(R.id.btn_2); btn_3 = (Button) findViewById(R.id.btn_3); btn_4 = (Button) findViewById(R.id.btn_4); btn_5 = (Button) findViewById(R.id.btn_5); btn_6 = (Button) findViewById(R.id.btn_6); btn_7 = (Button) findViewById(R.id.btn_7); btn_8 = (Button) findViewById(R.id.btn_8); btn_9 = (Button) findViewById(R.id.btn_9); btn_point = (Button) findViewById(R.id.btn_point); btn_del = (Button) findViewById(R.id.btn_del); btn_plus = (Button) findViewById(R.id.btn_plus); btn_clear = (Button) findViewById(R.id.btn_clear); btn_minus = (Button) findViewById(R.id.btn_minus); btn_multiply = (Button) findViewById(R.id.btn_multiply); btn_divide = (Button) findViewById(R.id.btn_divide); btn_equal = (Button) findViewById(R.id.btn_equal); 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_clear.setOnClickListener(this); btn_del.setOnClickListener(this); btn_plus.setOnClickListener(this); btn_minus.setOnClickListener(this); btn_multiply.setOnClickListener(this); btn_divide.setOnClickListener(this); btn_equal.setOnClickListener(this); et_input.setOnClickListener(this); } @Override public void onClick(View view) { String str = et_input.getText().toString(); switch (view.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(clear_flag){ clear_flag = false; str=""; et_input.setText(""); } et_input.setText(str+((Button)view).getText()); break; case R.id.btn_plus: case R.id.btn_minus: case R.id.btn_multiply: case R.id.btn_divide: et_input.setText(str+" "+((Button)view).getText()+" "); break; case R.id.btn_del: // if(clear_flag){ // clear_flag=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: // clear_flag = false; // str=""; et_input.setText(""); break; case R.id.btn_equal: getResult(); 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); 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(".") && !op.equals("÷")){ 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("")){ 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("÷")){ if(d2==0){ result = 0; }else { result = 0; } } if(!s2.contains(".")){ int r = (int)result; et_input.setText(r+""); }else{ et_input.setText(result+""); } }else{ et_input.setText(""); } }
添加回答
举报
0/150
提交
取消