我正在通过Android Studio构建迷你计算器,最后完成了。在这个程序中,我使用ButterKnife来@bind
这些视图更方便。当我运行计算器时问题就出现了,当我点击它们时,我无法从这些按钮接收任何响应。我尝试了尽可能多的方法,但它没有奏效。我检查了我的ButterKnife,它已经同步了。
这是我的代码:
package com.huy9515gmail.mycalculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@BindView(R.id.btn0)
Button btn0;
@BindView(R.id.btn1)
Button btn1;
@BindView(R.id.btn2)
Button btn2;
@BindView(R.id.btn3)
Button btn3;
@BindView(R.id.btn4)
Button btn4;
@BindView(R.id.btn5)
Button btn5;
@BindView(R.id.btn6)
Button btn6;
@BindView(R.id.btn7)
Button btn7;
@BindView(R.id.btn8)
Button btn8;
@BindView(R.id.btn9)
Button btn9;
@BindView(R.id.btnPlus)
Button btnPlus;
@BindView(R.id.btnMinus)
Button btnMinus;
@BindView(R.id.btnDup)
Button btnDup;
@BindView(R.id.btnDiv)
Button btnDiv;
@BindView(R.id.btnDot)
Button btnDot;
@BindView(R.id.btnAC)
Button btnAC;
@BindView(R.id.btnC)
Button btnC;
@BindView(R.id.btnEqual)
Button btnEqual;
@BindView(R.id.edt_input)
EditText edtInput;
@BindView(R.id.txt_Answer)
TextView txtAnswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);}
public void setEventClickViews() {
btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btnPlus.setOnClickListener(this);
btnMinus.setOnClickListener(this);
btnDup.setOnClickListener(this);
btnDiv.setOnClickListener(this);
btnAC.setOnClickListener(this);
btnC.setOnClickListener(this);
btnDot.setOnClickListener(this);
btnEqual.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn0:
edtInput.append("0");
break;
case R.id.btn1:
edtInput.append("1");
break;
case R.id.btn2:
edtInput.append("2");
break;
case R.id.btn3:
edtInput.append("3");
break;
case R.id.btn4:
edtInput.append("4");
break;
case R.id.btn5:
edtInput.append("5");
break;
case R.id.btn6:
edtInput.append("6");
break;
case R.id.btn7:
edtInput.append("7");
break;
case R.id.btn8:
edtInput.append("8");
break;
case R.id.btn9:
edtInput.append("9");
break;
case R.id.btnPlus:
edtInput.append("+");
break;
case R.id.btnMinus:
edtInput.append("-");
break;
case R.id.btnDup:
edtInput.append("x");
break;
case R.id.btnDiv:
edtInput.append(":");
break;
case R.id.btnAC:
edtInput.setText("");
txtAnswer.setText("");
break;
case R.id.btnC:
edtInput.setText(pressC(edtInput.getText().toString()));
break;
case R.id.btnDot:
edtInput.append(".");
break;
case R.id.btnEqual:
pressEqual();
break;
default: break;
}
}
public String pressC(String s) {
StringBuilder build = new StringBuilder(s);
build.deleteCharAt(build.length() - 1);
return build.toString();
}
public void pressEqual() {
boolean isDecimal = false;
String s = edtInput.getText().toString();
for (int i = 0; i <= s.length(); i++) {
if (s.charAt(i) == '.') isDecimal = true;
}
if (isDecimal) txtAnswer.setText(hasDecimalOperator(s));
else txtAnswer.setText(noDecimalOperator(s));
}
public String hasDecimalOperator(String s) {
float const1, const2, result = 0.0f;
String s1 = new String("");
String s2 = new String("");
int i = 0, target;
while ((s.charAt(i) != '+') || (s.charAt(i) != '-') || (s.charAt(i) != 'x') || (s.charAt(i) != ':')) {
s1 += s.charAt(i);
i++;
}
const1 = Float.parseFloat(s1);
target = i;
i++;
while (i <= s.length()) {
s2 += s.charAt(i);
i++;
}
const2 = Float.parseFloat(s2);
switch (s.charAt(target)) {
case '+':
result = const1 + const2;
case '-':
result = const1 - const2;
case 'x':
result = const1 * const2;
case ':':
result = const1 / const2;
}
return Float.toString(result);
}
public String noDecimalOperator(String s) {
int const1, const2;
long result = 0;
String s1 = new String("");
String s2 = new String("");
int i = 0, target;
while ((s.charAt(i) != '+') || (s.charAt(i) != '-') || (s.charAt(i) != 'x') || (s.charAt(i) != ':')) {
s1 += s.charAt(i);
i++;
}
const1 = Integer.parseInt(s1);
target = i;
i++;
while (i <= s.length()) {
s2 += s.charAt(i);
i++;
}
const2 = Integer.parseInt(s2);
switch (s.charAt(target)) {
case '+':
result = const1 + const2;
case '-':
result = const1 - const2;
case 'x':
result = const1 * const2;
case ':':
result = const1 / const2;
}
return Long.toString(result);
}
}
答案 0 :(得分:0)
您忘记在setEventClickViews ()
方法中调用onCreate
方法。