我在程序中設置了兩個 checkboxes 和兩個 EditText。點擊一個 CheckBox 時 EditText 消失,然後顯示Toast ,"Your Information is Saved",如何再次點擊 CheckBox時,另一個Toast 會顯示"Enter Your Email Address"。兩個toast在每次點擊後都可以正常運行。這是什麼原因?
on.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked())
receiverEmail.setVisibility(View.GONE);
if (((CheckBox) v).isChecked())
Toast.makeText(getApplicationContext(),
"Your Information is Saved", Toast.LENGTH_LONG).show();
else
receiverEmail.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(),
"Enter Your Email Address", Toast.LENGTH_LONG).show();
}
});
on2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked())
clientID.setVisibility(View.GONE);
if (((CheckBox) v).isChecked())
Toast.makeText(getApplicationContext(),
"Your Information is Saved", Toast.LENGTH_LONG).show();
else
clientID.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(),
"Enter Your Email Address", Toast.LENGTH_LONG).show();
}
});
if (((CheckBox) v).isChecked()){
Toast.makeText(getApplicationContext(),
"Your Information is Saved", Toast.LENGTH_LONG).show();
}
else{
receiverEmail.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(),
"Enter Your Email Address", Toast.LENGTH_LONG).show();
}
if else 如果省略{}的話只會執行臨近的一行代碼,第二行是不會執行的,你的問題應該是因為else後執行一行代碼receiverEmail.setVisibility(View.VISIBLE);,導致Toast.makeText(getApplicationContext(),
"Enter Your Email Address", Toast.LENGTH_LONG).show();無論如何都會執行的
樓主看下是不是因為這個。。。