繁体   English   中英

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

[英]Show AlertDialog “on top of” DialogPreference when ok is clicked

我有一个DialogPreference(更确切地说是EditTextPreference),并希望对用户输入的值进行一些检查。 这些检查应在用户单击确定时进行,而不是在键入时进行。 如果一切正常,对话框将关闭。 如果有错误,将显示一个AlertDialog并说明问题所在和确定按钮。 该AlertDialog将在DialogPreference对话框的顶部“视图”中显示,并且当单击“确定”按钮时,第一个对话框将再次出现。

我试图扩展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);
    }
}

但这并没有达到预期的效果:

  • 使用上面的代码,当单击EditTextPreference的肯定按钮时,将显示AlertDialog,并且不会保存该值,但是立即关闭EditTextPreference的对话框。
  • 如果是super.showDialog(new Bundle()); 没有注释,AlertDialog
    会显示出来,并在其上方立即显示EditTextPreference的
    对话框再次弹出。

那么我怎样才能达到期望的行为呢?

编辑:由于根据hackbod这是不可能的,我将使用一种接近的解决方案。 这远非良好的用户体验,但是由于我的应用程序将由不到100个人使用,并且我会在业余时间进行开发,因此,我不想为此付出太多努力-就像创建自己的DialogPreference一样。 这是我现在使用的:

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);
            }
        }
    }

}

抱歉,您不能执行此操作,在交付结果时,EditTextPreference会自动关闭其对话框,并且您不能阻止它执行此操作。

我建议您创建自己的DialogPreference,以便在点击时显示自己的自定义对话框。 您可以在该对话框中验证文本,因为它是您自己的对话框。 这也将为您带来更好的用户体验。

暂无
暂无

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

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