简体   繁体   English

单击确定时,在DialogPreference的顶部显示AlertDialog

[英]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. 我有一个DialogPreference(更确切地说是EditTextPreference),并希望对用户输入的值进行一些检查。 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. 如果有错误,将显示一个AlertDialog并说明问题所在和确定按钮。 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. 该AlertDialog将在DialogPreference对话框的顶部“视图”中显示,并且当单击“确定”按钮时,第一个对话框将再次出现。

I tried to extend EditTextPreference and override the onClick(DialogInterface dialog, int which) method to do this: 我试图扩展EditTextPreference并重写onClick(DialogInterface dialog,int which)方法来执行此操作:

@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. 使用上面的代码,当单击EditTextPreference的肯定按钮时,将显示AlertDialog,并且不会保存该值,但是立即关闭EditTextPreference的对话框。
  • If super.showDialog(new Bundle()); 如果是super.showDialog(new Bundle()); is uncommented, the AlertDialog 没有注释,AlertDialog
    is shown and above it immediately also the EditTextPreference's 会显示出来,并在其上方立即显示EditTextPreference的
    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. 编辑:由于根据hackbod这是不可能的,我将使用一种接近的解决方案。 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. 这远非良好的用户体验,但是由于我的应用程序将由不到100个人使用,并且我会在业余时间进行开发,因此,我不想为此付出太多努力-就像创建自己的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. 抱歉,您不能执行此操作,在交付结果时,EditTextPreference会自动关闭其对话框,并且您不能阻止它执行此操作。

I suggest making your own DialogPreference that displays a custom dialog of your own when tapped. 我建议您创建自己的DialogPreference,以便在点击时显示自己的自定义对话框。 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. 这也将为您带来更好的用户体验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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