单击“正”按钮时,不会忽略Alertdialog,直到条件为真

时间:2014-07-28 05:31:20

标签: android android-alertdialog

我正在使用警告对话框将用户登录到应用程序的设置,但即使条件为假,dilog也会继续贬低 这是代码

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);
            }

                            }
                       }
                   });

在错误的情况下,它会显示错误一秒钟,然后是dimisses,我也尝试了

final AlertDialog alertdialog = builder.create();
alertdialog.setCancelable(false);

但它没有用。

1 个答案:

答案 0 :(得分:3)

    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();