我正在使用从对话框扩展的类来更改密码。当我按下是按钮时,对话框被关闭。如果输入错误,我希望对话框不会关闭。
这是我的代码
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
if ((!TextUtils.isEmpty(newpass.getText().toString())) && oldpass.getText().toString().equals(Login_Activity.e.getPassword())) {
Login_Activity.e.password=newpass.getText().toString();
user.child(Login_Activity.e.getId()).setValue(Login_Activity.e);
dismiss();
} else {
yes.setBackgroundResource(R.color.red);
}
case R.id.btn_no:
dismiss();
break;
}
}
感谢帮助
最佳答案
您需要为 case R.id.btn_yes
添加 break
语句。根据docs :
The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered
在您的情况下,单击 ID 为 btn_yes
的按钮,然后执行 if
或 else
中的代码块,然后程序流程继续在 case R.id.btn_no
中执行代码,因为在匹配的 case 中它没有被 break 停止。
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
if ((!TextUtils.isEmpty(newpass.getText().toString())) && oldpass.getText().toString().equals(Login_Activity.e.getPassword())) {
Login_Activity.e.password=newpass.getText().toString();
user.child(Login_Activity.e.getId()).setValue(Login_Activity.e);
dismiss();
} else {
yes.setBackgroundResource(R.color.red);
}
break; // add this
case R.id.btn_no:
dismiss();
break;
}
}
关于android - 对话框在是按钮上关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54850045/