如何让AlertDialog 在点击确定或者取消时不消失

查看AlertDialog的源代码,如下:

 /**
         * Set a listener to be invoked when the positive button of the dialog is pressed.
         * @param textId The resource id of the text to display in the positive button
         * @param listener The {@link DialogInterface.OnClickListener} to use.
         *
         * @return This Builder object to allow for chaining of calls to set methods
         */
        public Builder setPositiveButton(int textId, final OnClickListener listener) {
            P.mPositiveButtonText = P.mContext.getText(textId);
            P.mPositiveButtonListener = listener;
            return this;
        }
               
        public Builder setPositiveButton(CharSequence text, final OnClickListener listener) {
            P.mPositiveButtonText = text;
            P.mPositiveButtonListener = listener;
            return this;
        }
       
        public Builder setNegativeButton(int textId, final OnClickListener listener) {
            P.mNegativeButtonText = P.mContext.getText(textId);
            P.mNegativeButtonListener = listener;
            return this;
        }
              
        public Builder setNegativeButton(CharSequence text, final OnClickListener listener) {
            P.mNegativeButtonText = text;
            P.mNegativeButtonListener = listener;
            return this;
        }


注意看注释第一句:Set a listener to be invoked when the positive button of the dialog is pressed,这个Listener是监听button被点击事件。我们的目的是让AlertDialog 在点击确定或者取消时不消失,那么屏蔽此Listener即可。

解决方法:

继承AlertDialog自定义自己的AlertDialog,重写以上四个方法:(把这四个方法拷贝过来,都不要这两句:
P.mPositiveButtonListener = listener;
P.mNegativeButtonListener = listener;)

此外还有不常用的这个按钮,如法炮制。
public Builder setNeutralButton(int textId, final OnClickListener listener)
public Builder setNeutralButton(CharSequence text, final OnClickListener listener)


其他不同的解决方法:http://blog.csdn.net/abby_dcy/article/details/6049535

如何让AlertDialog 在点击确定或者取消时不消失

上一篇:平衡二叉树--c#求解--英雄会在线编程题目


下一篇:深入理解java虚拟机【Tomcat类加载器体系结构】