Android 工作室中的复选框
CheckBoxes in Android studio
我有这段代码,在我的程序中我使用了复选框。如果复选框被选中,它必须执行 if 语句中的语句,但我的程序执行所有 "if statements" 即使复选框未被选中。
public void onClickMakeTransactionButton(){
chkAirtime = (CheckBox) findViewById(R.id.chkAirtime);
chkElectricity = (CheckBox) findViewById(R.id.chkElectricity);
chkWater = (CheckBox) findViewById(R.id.chkWater);
chkTransfer = (CheckBox) findViewById(R.id.chkTransfers);
chkWithdrawal = (CheckBox) findViewById(R.id.chkWithdrawal);
chkPayDstv = (CheckBox) findViewById(R.id.chkPayDstv);
final TextView savingsBalance = (TextView) findViewById(R.id.txtSavingsBalance);
savingsBalance.setText("Your Balance is: " + balance);
btnMakeTransaction = (Button) findViewById(R.id.btnMakeTransaction);
btnMakeTransaction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
//This are the if statements for my check boxes. My program executes the if statements even if the check boxes are not checked. what could be the problem. please help
if (result == result.append("Airtime: ").append(chkAirtime.isChecked())) {
balance = balance - 50;
}
if (result == result.append("Electricity: ").append(chkElectricity.isChecked())) {
balance = balance - 150;
}
if (result == result.append("Water: ").append(chkWater.isChecked())) {
balance = balance - 150;
}
if (result == result.append("Transfers: ").append(chkTransfer.isChecked())) {
balance = balance - 500;
}
if (result == result.append("Withdrawal: ").append(chkWithdrawal.isChecked())) {
balance = balance - 200;
}
if (result == result.append("Pay Dstv: ").append(chkPayDstv.isChecked())) {
balance = balance - 200;
} else {
Toast.makeText(SavingsAccountTransactions.this, "Nothing been Selected ", Toast.LENGTH_LONG).show();
}
savingsBalance.setText("Your Balance is: " + balance);
Toast.makeText(SavingsAccountTransactions.this, result.toString(), Toast.LENGTH_SHORT).show();
}
});
}
显然,如果条件 returns 始终为真。
(示例:result == result.append("Airtime: ").append(chkAirtime.isChecked())
)
为什么 : 你在 if 条件下使用相同的对象。
通过查看您的代码,我认为您需要像这样检查
if (chkAirtime.isChecked()) { //checking CheckBox is checked or not.
balance = balance - 50;
result.append("Airtime: "+balance+" ");//appending data to result
}
对所有其他 if 条件执行相同的操作。
重要提示:如果要比较两个字符串,请始终使用 equals
。喜欢 s1.equals(s2);
visit this : SO - Java String.equals versus ==
我有这段代码,在我的程序中我使用了复选框。如果复选框被选中,它必须执行 if 语句中的语句,但我的程序执行所有 "if statements" 即使复选框未被选中。
public void onClickMakeTransactionButton(){
chkAirtime = (CheckBox) findViewById(R.id.chkAirtime);
chkElectricity = (CheckBox) findViewById(R.id.chkElectricity);
chkWater = (CheckBox) findViewById(R.id.chkWater);
chkTransfer = (CheckBox) findViewById(R.id.chkTransfers);
chkWithdrawal = (CheckBox) findViewById(R.id.chkWithdrawal);
chkPayDstv = (CheckBox) findViewById(R.id.chkPayDstv);
final TextView savingsBalance = (TextView) findViewById(R.id.txtSavingsBalance);
savingsBalance.setText("Your Balance is: " + balance);
btnMakeTransaction = (Button) findViewById(R.id.btnMakeTransaction);
btnMakeTransaction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
//This are the if statements for my check boxes. My program executes the if statements even if the check boxes are not checked. what could be the problem. please help
if (result == result.append("Airtime: ").append(chkAirtime.isChecked())) {
balance = balance - 50;
}
if (result == result.append("Electricity: ").append(chkElectricity.isChecked())) {
balance = balance - 150;
}
if (result == result.append("Water: ").append(chkWater.isChecked())) {
balance = balance - 150;
}
if (result == result.append("Transfers: ").append(chkTransfer.isChecked())) {
balance = balance - 500;
}
if (result == result.append("Withdrawal: ").append(chkWithdrawal.isChecked())) {
balance = balance - 200;
}
if (result == result.append("Pay Dstv: ").append(chkPayDstv.isChecked())) {
balance = balance - 200;
} else {
Toast.makeText(SavingsAccountTransactions.this, "Nothing been Selected ", Toast.LENGTH_LONG).show();
}
savingsBalance.setText("Your Balance is: " + balance);
Toast.makeText(SavingsAccountTransactions.this, result.toString(), Toast.LENGTH_SHORT).show();
}
});
}
显然,如果条件 returns 始终为真。
(示例:result == result.append("Airtime: ").append(chkAirtime.isChecked())
)
为什么 : 你在 if 条件下使用相同的对象。
通过查看您的代码,我认为您需要像这样检查
if (chkAirtime.isChecked()) { //checking CheckBox is checked or not.
balance = balance - 50;
result.append("Airtime: "+balance+" ");//appending data to result
}
对所有其他 if 条件执行相同的操作。
重要提示:如果要比较两个字符串,请始终使用 equals
。喜欢 s1.equals(s2);
visit this : SO - Java String.equals versus ==