Android Studio实现计算器,运行后点击计算器上的键没有反应,求大神看看代码哪出错了。。。
public class MainActivity extends AppCompatActivity {
private Button button1, button2, button3, button_div, button_7, button_8, button_9, button_mul, button_4, button_5, button_6, button_sub, button_1, button_2, button_3, button_add, button_0, button_dot, button_equal;
private EditText et_res;
private final String TAG = "CAL";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button_div = (Button) findViewById(R.id.button_div);
button_7 = (Button) findViewById(R.id.button_7);
button_8 = (Button) findViewById(R.id.button_8);
button_9 = (Button) findViewById(R.id.button_9);
button_4 = (Button) findViewById(R.id.button_4);
button_5 = (Button) findViewById(R.id.button_5);
button_6 = (Button) findViewById(R.id.button_6);
button_1 = (Button) findViewById(R.id.button_1);
button_2 = (Button) findViewById(R.id.button_2);
button_3 = (Button) findViewById(R.id.button_3);
button_add = (Button) findViewById(R.id.button_add);
button_0 = (Button) findViewById(R.id.button_0);
button_dot = (Button) findViewById(R.id.button_dot);
button_equal = (Button) findViewById(R.id.button_equal);
button_mul = (Button) findViewById(R.id.button_mul);
button_sub = (Button) findViewById(R.id.button_sub);
et_res = (EditText) findViewById(R.id.et_res);
}
private class MyOnClickLis implements View.OnClickListener {
private String str1 = "0";//第一个操作数
private String str2 = "";//第二个操作数
private String oper = "";//当前的运算符
private String strResult = "0";//当前显示的结果
private int lastInput = 0;//最后输入的值,0=数字,1=运算符
private void numInput(int num) {
if (strResult.equals("0")) {
strResult = " " + num;
} else if (lastInput == 0) {
strResult = strResult + num;
} else if (lastInput == 1) {
strResult = " " + num;
}
et_res.setText(strResult);
lastInput = 0;
}
private void dotInput() {
if (strResult.indexOf(".") == -1) {
strResult = strResult + ".";
et_res.setText(strResult);
}
}
private void operationInput(String op) {
if (lastInput == 1) {
oper = op;
lastInput = 1;
return;
}
if (oper.isEmpty()) {
oper = op;
str1 = strResult;
} else if (!str1.isEmpty()) {
str2 = strResult;
}
if (op.equals("+")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
strResult = String.valueOf(op1 + op2);
et_res.setText(strResult);
str1 = strResult;
oper = op;
} else if (op.equals("-")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
strResult = String.valueOf(op1 - op2);
et_res.setText(strResult);
str1 = strResult;
oper = op;
} else if (op.equals("*")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
strResult = String.valueOf(op1 * op2);
et_res.setText(strResult);
str1 = strResult;
oper = op;
} else if (op.equals("/")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
if (op2 == 0) {
et_res.setText("错误");
} else if (op2 != 0) {
strResult = String.valueOf(op1 / op2);
et_res.setText(strResult);
str1 = strResult;
oper = op;
}
}
}
private void percentInput () {
double op1 = Double.parseDouble(strResult);
strResult = Double.toString(op1 / 100);
et_res.setText(strResult);
}
private void acInput(){
strResult = "0";
lastInput = 0;
oper = "";
str1 = str2 = "0";
et_res.setText(strResult);
}
private void psInput(){
double op1=Double.parseDouble(strResult);
strResult=Double.toString(-op1);
et_res.setText(strResult);
}
private void equalInput(){
if (oper.isEmpty()) {
return;
}
if (lastInput == 1 && !str1.isEmpty()) {
if (oper.equals("+")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(strResult);
strResult = String.valueOf(op1 + op2);
et_res.setText(strResult);
} else if (oper.equals("-")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(strResult);
strResult = String.valueOf(op1 - op2);
et_res.setText(strResult);
} else if (oper.equals("*")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(strResult);
strResult = String.valueOf(op1 * op2);
et_res.setText(strResult);
} else if (oper.equals("/")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(strResult);
strResult = String.valueOf(op1 / op2);
et_res.setText(strResult);
}
} else if (lastInput == 0 && !str2.isEmpty()) {
if (oper.equals("+")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
strResult = String.valueOf(op1 + op2);
et_res.setText(strResult);
} else if (oper.equals("-")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
strResult = String.valueOf(op1 - op2);
et_res.setText(strResult);
} else if (oper.equals("*")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
strResult = String.valueOf(op1 * op2);
et_res.setText(strResult);
} else if (oper.equals("/")) {
double op1 = Double.parseDouble(str1);
double op2 = Double.parseDouble(str2);
strResult = String.valueOf(op1 / op2);
et_res.setText(strResult);
}
}
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.button_0:
numInput(0);
break;
case R.id.button_1:
numInput(1);
break;
case R.id.button_2:
numInput(2);
break;
case R.id.button_3:
numInput(3);
break;
case R.id.button_4:
numInput(4);
break;
case R.id.button_5:
numInput(5);
break;
case R.id.button_6:
numInput(6);
break;
case R.id.button_7:
numInput(7);
break;
case R.id.button_8:
numInput(8);
break;
case R.id.button_9:
numInput(9);
break;
case R.id.button_add:
operationInput("+");
break;
case R.id.button_sub:
operationInput("-");
break;
case R.id.button_mul:
operationInput("*");
break;
case R.id.button_div:
operationInput("/");
break;
case R.id.button1:
acInput();
break;
case R.id.button2:
psInput();
break;
case R.id.button3:
percentInput();
break;
case R.id.button_dot:
dotInput();
break;
case R.id.button_equal:
equalInput();
break;
}
}
}
}
第一、你的代码有点乱,不利于阅读;
第二、整体代码应该调整一下,按钮太多了,应该在public class MainActivity extends AppCompatActivity implements View.OnClickListener,并重写onClick事件,也就是你定义的那些方法;
第三、不应该写一个整体的自定义点击事件监听;
第四、你没有给所有可点击的button设置setOnclickListener()事件
一、
我觉得哈,**View.onClickListener**应该放在**MainActivity的后面**。这些Activity都继承了AppComponentActivity并且都是在Manifest.xml中注册过和对应的layout布局文件联系起来的,这样才能让UI布局响应设置,没有必要单独再建一个类来设置按钮的响应事件。(虽然我没有这么试过,但是这样很容易出问题或者按钮无法响应,我觉得是不能这样做的)。应该如下实现**View.onClickListener**:
(public class MainActivity extends AppCompatActivity implements View.onClickListener)。
二、
这些响应事件放在Activity类中的重写方法**onResume()**中就行了(你在MainActivity类中的空白处输入**onResume**编译器就会自动填补上该方法,建议可以再看一下有关安卓活动生命周期的知识)
三、
最关键的是你的每个Button都要设置一下.setOnClickListener();
这样每个Button才会响应点击事件。如果你采用了前面所说的MainActivity extends AppCompatActivity implements View.onClickListener形式,那么你可以在onCreate()方法中对每个按钮都设置.setOnClickListener(**this**)。
这样应该是可以的,希望能帮到你。
绑定控件之后没有绑定控件的 setOnClick 事件