2017-07-18 90 views
0

我正在通過Android Studio構建一個迷你計算器,並最終完成。在這個程序中,我使用ButterKnife來使@視圖更加方便。當我運行計算器時,問題就開始了,當我點擊它們時,我無法從這些按鈕收到任何迴應。我嘗試儘可能多的方式,但它沒有奏效!我檢查了我的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

你忘了打電話給在onCreate方法你setEventClickViews()方法。

+0

Omg :))))多麼荒謬的錯誤:v 無論如何,謝謝你的幫助<3 –