开发者

DialogPreference shouldn't close if no option selected

I have a DialogPreference and I want to avoid the user from closing it when pressing "OK", "Cancel", etc.

How should I do that?

EDIT:

I tried to开发者_高级运维 reach the OK button to disable when the dialog is created. But I couldn't make it :(


The solution is quite easy. Overwrite showDialog and set your own click listener to the buttons you want to intercept.

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

In your click listener you can do the validation you want.


A tweak could be to create a custom dialog where 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;
        }
    }
}

Check out http://developer.android.com/reference/android/content/SharedPreferences.Editor.html in order to handle your preference in onClick. I didn't test this code just wrote it to show you how you could solve it!

The dialog stays open until you call dialog.dismiss();. In that case you'll have to create your drop-down-menu, polls or what ever you want to display in your layout file. After pressing ok or cancel you should check if the user made a choice, and parse that choice into your preferences. (check link above)

Rgds Layne


You could try opening it again.

Why would you want to prevent users to close the dialog? Users should be able to have 'full' control of their device.


You can see the source code of DialogPreferences here:
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/preference/DialogPreference.java
And then, copy most of it to your code, modifying the code as needed.


How about overriding the onDismiss() method and implementing a canExit() method with the validations you want to occcur? E.g. :

public class MyDialogPref extends DialogPreference {

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


A good UI should have a default selection/option already selected (the previously user-entered options or a program default).

Presenting a dialog asking for a change in options without any indication of what you already have is bad UI design.

This way if the user clicks Cancel, nothing changes and they saw what the option selected was. If they make no change and click OK then nothing really changes either.

Software is supposed to make doing specific tasks easier, not force the user to process the apps logic themselves.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜