How to cancel closing the settings dialog after pressing the positive button?

How can I cancel the aftet closing settings dialog by pressing any button?

I decided to make my dialog class implement OnClickListener.

public class PassChangeDiglog extends DialogPreference implements
        OnClickListener {
    EditText oldpass, new_pass1, new_pass2;

    public PassChangeDiglog(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDialogLayoutResource(R.layout.pass_change_diglog);
        setPositiveButtonText("OK");
        setNegativeButtonText(R.string.Cancel);

    }


@Override
protected void onBindDialogView(View view) {
    setTitle(R.string.PassChanging);
    oldpass = (EditText) view.findViewById(R.id.Dlg_old_pass);
    new_pass1 = (EditText) view.findViewById(R.id.Dlg_NewPass1);
    new_pass2 = (EditText) view.findViewById(R.id.Dlg_NewPass2);

    super.onBindDialogView(view);
}


    @Override
    public void onClick(DialogInterface arg0, int arg1) {
        switch (arg1) {
        case DialogInterface.BUTTON_POSITIVE:
           boolean needclose;
           ...
           if (needclose)
               arg0.dismiss();
           else{
               //do not close
           }
        }

    };
}

I tried to override the onDismiss method, but the dialog closes.

@Override
public void onDismiss(DialogInterface dialog) {
    // TODO Auto-generated method stub
    Log.d("MY","onDismiss");
    //super.onDismiss(dialog);
}
+3
source share
2 answers

You need to add another listener after, your dialogue is already shown.

    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
          new View.OnClickListener()
          {            
              @Override
              public void onClick(View view)
              {
                 // do your magic
              }
          });

To make it work with preference, use cast to AlertDialog: (AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE)

0
source

The solution to my question is to override the showDialog method.

@Override
protected void showDialog(Bundle state) {
    // TODO Auto-generated method stub
    super.showDialog(state);
    ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE)
            .setOnClickListener(this);
}
+3
source

All Articles