我正在使用警报对话框将用户登录到应用程序的设置中,但dilog会继续关闭,即使条件为false
这是密码
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if(!etxtdlgPass.getText().toString().equals("") && etxtdlgPass.getText().toString().trim().equals(SETTINGS_PAWSSWORD)){
if(isLoginRequest){
Intent intent = new Intent(ctx, AppPreferenceActivity.class);
intent.putExtra(Common.LOGINREQUEST, Common.LOGINREQUEST);
if(rgLanguage.getCheckedRadioButtonId() == R.id.rbtnEngLang){
intent.putExtra(Common.LANGREQUEST, Locale.ENGLISH.toString());
}else{
intent.putExtra(Common.LANGREQUEST, Locale.JAPANESE.toString());
}
ctx.startActivity(intent);
}else{
Intent intent = new Intent(ctx, AppPreferenceActivity.class);
ctx.startActivity(intent);
}
tvwdlgPass_Error.setVisibility(View.GONE);
tvwdlgPass_Error.setText("");
dialog.cancel();
}else{
String strPasswordError = "";
tvwdlgPass_Error.setVisibility(View.VISIBLE);
if(isLoginRequest){
strPasswordError = "enter correct Password";
tvwdlgPass_Error.setText(strPasswordError);
}else{
strPasswordError ="error Here"
tvwdlgPass_Error.setText(strPasswordError);
}
}
}
});
在错误情况下,它会显示一秒钟的错误,但随后会变小,我也尝试过
final AlertDialog alertdialog = builder.create();
alertdialog.setCancelable(false);
但没用。
最佳答案
builder.setCancelable(false);
builder.setPositiveButton("Login", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {// Do not write your logic.If you write it will automatically dismiss the dialog. Instead of here handle onShow() like below.
}
});
builder.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Write your Logic. It will never dismiss the dialog unless your condition satisifies
}
});
alertDialog.show();
关于android - 在条件为真之前,单击“正向”按钮时不能使Alertdialog无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24988913/