剛開始學習Android編碼。我知道Java的基礎知識,並且正在改進它。瞭解到按鈕和OnclickListeners
,所以我想開發一個簡單的程序來使用按鈕,並設計了一個計算器。如果我的邏輯是正確的,請幫助我。當我按等於(beq
按鈕)它不起作用!Android初學者 - OnclickListeners
int a, b, total,OP;
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, badd, bsub, bmul, bdiv, bclr, beq;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);
b5 = (Button) findViewById(R.id.b5);
b6 = (Button) findViewById(R.id.b6);
b7 = (Button) findViewById(R.id.b7);
b8 = (Button) findViewById(R.id.b8);
b9 = (Button) findViewById(R.id.b9);
b0 = (Button) findViewById(R.id.b0);
badd = (Button) findViewById(R.id.add);
bsub = (Button) findViewById(R.id.sub);
bmul = (Button) findViewById(R.id.div);
bdiv = (Button) findViewById(R.id.mul);
bclr = (Button) findViewById(R.id.clr);
beq = (Button) findViewById(R.id.eq);
et = (EditText) findViewById(R.id.et);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et.equals("")) {
et.setText("1");
} else {
et.append("1");
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et.equals("")) {
et.setText("2");
} else {
et.append("2");
}
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et.equals("")) {
et.setText("3");
} else {
et.append("3");
}
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et.equals("")) {
et.setText("4");
} else {
et.append("4");
}
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et.equals("")) {
et.setText("5");
} else {
et.append("5");
}
}
});
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et.equals("")) {
et.setText("6");
} else {
et.append("6");
}
}
});
b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et.equals("")) {
et.setText("7");
} else {
et.append("7");
}
}
});
b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et.equals("")) {
et.setText("8");
} else {
et.append("8");
}
}
});
b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et.equals("")) {
et.setText("9");
} else {
et.append("9");
}
}
});
b0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et.equals("")) {
et.setText("0");
}
else {
et.append("0");
}
}
});
badd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a = Integer.parseInt(et.getText().toString());
et.setText("");
OP = 1;
}
});
bsub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a = Integer.parseInt(et.getText().toString());
et.setText("");
OP = 2;
}
});
bdiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a = Integer.parseInt(et.getText().toString());
et.setText("");
OP = 3;
}
});
bmul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a = Integer.parseInt(et.getText().toString());
et.setText("");
OP = 4;
}
});
bclr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et.setText("");
a=0;
b=0;
total=0;
OP=0;
}
});
beq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (OP){
case 1:
b= Integer.parseInt(et.getText().toString());
total=a+b;
et.setText(String.format("%d",total));
break;
case 2:
b= Integer.parseInt(et.getText().toString());
total=a-b;
et.setText(String.format("%d",total));
break;
case 3:
b= Integer.parseInt(et.getText().toString());
total=a/b;
et.setText(String.format("%d",total));
break;
case 4:
b= Integer.parseInt(et.getText().toString());
total=a*b;
et.setText(String.format("%d",total));
break;
}
}
});
}
}
在'的onClick()''爲beq',你能檢查'total'值。只要做'System.out.println(total);'。結果將在'System.out'下的logcat中。 –
您可以嘗試以下一種或多種方法;添加一個斷點並進行調試;在交換機之前添加一個Toast檢查OP a和b的值;添加默認發佈敬酒。您可以使用類似於吐司的方式使用日誌調用。我的猜測是OP最終不是1,2,3或4. – MikeT