DialogPreference should not be closed if the option is not selected

I have a DialogPreference, and I want the user to not close it when I click OK, Cancel, etc.

How should I do it?

EDIT:

I tried to click OK to disconnect when creating a dialog. But I could not do this :(

+3
source share
6 answers

Customization may consist of creating a custom dialog in which you define your own buttons (OK and Close).

public class YourClass implements OnClickListener {
    private Button DialogButton;
    private Dialog dialog;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.MainLayout);

        /* Your code... */

        DialogButton = (Button) findViewById(R.id.DialogButtonId);
        DialogButton.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.DialogButtonId:
            LayoutInflater inflater = LayoutInflater.from(YourClass.this);
            final View inflay = inflater.inflate(R.layout.DialogLayout, (ViewGroup) findViewById(R.id.RootIdOfDialogLayout));

            TextView YourTextView = (TextView) inflay.findViewById(R.id.TextViewId);

            Button cancel = (Button) inflay.findViewById(R.id.CancelButtonId);      
            cancel.setOnClickListener(YourClass.this);

            Button ok = (Button) inflay.findViewById(R.id.OkButtonId);      
            ok.setOnClickListener(YourClass.this);

            dialog = new Dialog(YourClass.this);
            dialog.setContentView(inflay);
            dialog.setTitle(getString(R.string.TitleStringId));
            dialog.show();
            break;
        case R.id.CancelButtonId:
            /* Checking if the user selected an option if true call dialog.dismiss() */
            break;
        case R.id.OkButtonId:
            /* Here handle your preferences (e.g. putString(String key, String value)) */
            /* Checking if the user selected an option if true call dialog.dismiss() */
            break;
        }
    }
}

http://developer.android.com/reference/android/content/SharedPreferences.Editor.html, onClick. , , , !

, dialog.dismiss();. , , . ok cancel , , . ( )

Rgds Layne

+3

. showDialog , .

   @Override
    protected void showDialog(Bundle bundle) {
        super.showDialog(bundle);
        Button pos = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
        pos.setOnClickListener(...);
    }

.

+8

.

? .

+1

onDismiss() canExit() , ? .

public class MyDialogPref extends DialogPreference {

  @override public void onDismiss(DialogInterface dialog) {
    if (canExit()) {
      super.onDismiss(dialog);
    }
  }
  ...
}
0

/, ( ).

, , .

, "", , , . "", .

, .

0

Source: https://habr.com/ru/post/1730914/


All Articles