简体   繁体   中英

How to keep DialogPreference open onClick

I am trying to figure out how in my custom DialogPreference, on click of positive_button, i can perform a check, and if it is not valid, then leave the dialog open. If it is valid, I want to dismiss the custom DialogPreference

The following is what I currently have, but not matter if it works or not, it closes the dialog onClick

public class PasswordDialogPreference extends DialogPreference {

    TextView tvOldPassword;
    TextView tvNewPassword;
    TextView tvConfirmPassword;

    EditText etOldPassword;
    EditText etNewPassword;
    EditText etConfirmPassword;

    SharedPreferences sharedPrefs;
    SharedPreferences.Editor editor;

    public PasswordDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        setDialogLayoutResource(R.layout.password_dialog_preference);
        setPositiveButtonText(android.R.string.ok);
        setNegativeButtonText(android.R.string.cancel);
        setDialogIcon(null);
        setPersistent(false);
    }

    @Override
    public void onBindDialogView(View view) {
        tvOldPassword = (TextView) view.findViewById(R.id.tvOldPassword);
        tvNewPassword = (TextView) view.findViewById(R.id.tvNewPassword);
        tvConfirmPassword = (TextView) view.findViewById(R.id.tvConfirmPassword);

        etOldPassword = (EditText) view.findViewById(R.id.etOldPassword);
        etNewPassword = (EditText) view.findViewById(R.id.etNewPassword);
        etConfirmPassword = (EditText) view.findViewById(R.id.etConfirmPassword);

        sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(getContext());


        if (sharedPrefs.getString("prefPasscode", "").length() < 4) {
            tvOldPassword.setVisibility(View.GONE);
            etOldPassword.setVisibility(View.GONE);
        }
        super.onBindDialogView(view);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        persistBoolean(positiveResult);
    }

    @Override
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
        super.onPrepareDialogBuilder(builder);
        //builder.setNegativeButton(null, null);

        builder.setPositiveButton(AlertDialog.BUTTON_POSITIVE, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                sharedPrefs = PreferenceManager
                        .getDefaultSharedPreferences(getContext());

                if (etNewPassword.getText().toString().equals(etConfirmPassword.getText().toString())) {
                    editor.putString("prefPasscode", etConfirmPassword.getText().toString());
                    editor.commit();
                    Toast.makeText(getContext(), "Pin saved.",
                            Toast.LENGTH_SHORT
                    ).show();
                } else if (etNewPassword.getText().toString().length() !=  4) {
                    Toast.makeText(getContext(), "Pin must be 4 digits.",
                            Toast.LENGTH_SHORT
                    ).show();
                } else if (!etNewPassword.getText().toString().equals(etConfirmPassword.getText().toString())) {
                    Toast.makeText(getContext(), "Pin does not match.",
                            Toast.LENGTH_SHORT
                    ).show();
                }
            }
        });
    }

}

Try to override showDialog(Bundle) method to get the actual AlertDialog instance and reset onClickListener for positive button:

@Override
protected void showDialog(Bundle state) {
    super.showDialog(state);

    final AlertDialog dlg = (AlertDialog) getDialog();
    dlg.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (needToClose) {
                dlg.dismiss();
            }
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM