开发者

Show AlertDialog "on top of" DialogPreference when ok is clicked

I've got a DialogPreference (more precisely an EditTextPreference) and want to perform some checks on the value the user has input. These checks shall be made when the user clicks ok, not already while he is typing. If everything is ok, the dialog shall close. If there is an error, an AlertDialog shall appear with an explanation what is wrong and an ok-button. This AlertDialog shall come into view "on top of" the DialogPreference's dialog, and when the ok-button is clicked, the first dialog shall come into view again.

I tried to extend EditTextPreference and override the onClick(DialogInterface dialog, int which) method to do this:

@Override
public void onClick(DialogInterface dialog, int which) {
    boolean invalidData = false;
    // check input
    if (true) {
        invalidData = true;
    }
    if (which == DialogInterface.BUTTON_POSITIVE && invalidData) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setMessage("Some message.")
        .setCancelable(false)
        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        }).create().show();
        //super.showDialog(new Bundle());
    } else {
        super.onClick(dialog, which);
    }
}

But this does not have the desired effect:

  • With the code above, the AlertDialog is shown and the value is not saved when the EditTextPreference's positive button is clicked, but the EditTextPreference's dialog is immediately closed.
  • If super.showDialog(new Bundle()); is uncommented, the AlertDialog

    is shown and above it immediately also the EditTextPreference's

    dialog pops up again.

So how can I reach the desired behaviour?开发者_运维技巧

EDIT: As according to hackbod this is not possible, I will use a solution that gets close. This is far from a good user experience, but as my app will be used by less than 100 people and I develop this in my spare time, I don't want to put too much effort in it - like creating my own DialogPreference. This is what I use now:

public abstract class EditTextPreferenceWithCheck extends EditTextPreference {

    private boolean mAlertDialogActive;
    private String mCachedValue;
    private String mMessage = "";

    public EditTextPreferenceWithCheck(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public EditTextPreferenceWithCheck(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EditTextPreferenceWithCheck(Context context) {
        super(context);
    }

    /**
     * Sets the message that will be shown if inputIsValid(String input) returns
     * false.
     * 
     * @param message The message to show
     */
    protected void setMessage(String message) {
        this.mMessage = message;
    }

    /**
     * Checks if the user input is valid. If not, the message set with
     * setMessage(String message) will be shown.
     * 
     * @param input Current value in the text field
     * @return true if the current value in the text field is valid, otherwise
     *         false
     */
    protected abstract boolean inputIsValid(String input);

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (mAlertDialogActive) {
            mAlertDialogActive = false;
            showDialog(new Bundle());
            getEditText().setText(mCachedValue);
        } else {
            if (which == DialogInterface.BUTTON_POSITIVE
                    && !inputIsValid(getEditText().getText().toString())) {
                mAlertDialogActive = true;
                mCachedValue = getEditText().getText().toString();
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setMessage(mMessage)
                        .setCancelable(false)
                        .setPositiveButton(android.R.string.ok, this).show();
            } else {
                super.onClick(dialog, which);
            }
        }
    }

}


Sorry, you can't do this, EditTextPreference automatically dismisses its dialog when delivering a result and you can't stop it from doing that.

I suggest making your own DialogPreference that displays a custom dialog of your own when tapped. You can do the verification of the text inside that dialog since it is your own dialog. This will also give you a better user experience.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜