美文网首页
ProgressDialog 使用心得

ProgressDialog 使用心得

作者: 南窗云 | 来源:发表于2018-06-27 12:24 被阅读0次

    遇到一个需求,现在新版本的apk过程中,断网与恢复网络需要控制一个button的隐现

    选择使用 Android 原生的 ProgressDialog

    ProgressDialog 继承 AlertDialog

    /**
     * A dialog showing a progress indicator and an optional text message or view.
     * Only a text message or a view can be used at the same time.
     *
     * <p>The dialog can be made cancelable on back key press.</p>
     *
     * <p>The progress range is 0 to {@link #getMax() max}.</p>
     *
     * @deprecated <code>ProgressDialog</code> is a modal dialog, which prevents the
     * user from interacting with the app. Instead of using this class, you should
     * use a progress indicator like {@link android.widget.ProgressBar}, which can
     * be embedded in your app's UI. Alternatively, you can use a
     * <a href="/guide/topics/ui/notifiers/notifications.html">notification</a>
     * to inform the user of the task's progress.
     */
    @Deprecated
    public class ProgressDialog extends AlertDialog {
    

    使用过程

    // 声明
        private var mDialog: ProgressDialog? = null
    // 在适当的位置初始化
        mDialog = ProgressDialog(mContext)
    

    显示并设置相关属性

        private fun showProgressDialog(str: String, progress: Int) {
            mDialog!!.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)
            mDialog!!.setTitle("新版本下载")
            mDialog!!.setMessage(str)
            mDialog!!.max = 100
            mDialog!!.setCancelable(false)
            mDialog!!.progress = progress
            mDialog!!.setButton(BUTTON_POSITIVE,"点我重试") { dialog, which -> ToastUtils.showShortToast(mContext,"点我干嘛?") }
            mDialog!!.show()
    //      网络正常情况下,先隐藏掉,必须在show()方法之后进行调用,否则
            mDialog!!.getButton(BUTTON_POSITIVE).visibility = View.GONE
        }
    

    注意的地方

    1. 设置进度,直接修改 mDialog!!.progress 的值就可以了
      private fun setProgress(progress: Int) {
            mDialog!!.progress = progress
            if (progress == 100) {
                mDialog!!.dismiss()
            }
        }
    
    1. 需要注意的地方,对属性值的修改都必须在 在show()方法之后进行调用,否则
    java.lang.IllegalArgumentException: Button does not exist
    

    关于Button

    源码

        /**
         * Set a listener to be invoked when the positive button of the dialog is pressed.
         *
         * @param whichButton Which button to set the listener on, can be one of
         *            {@link DialogInterface#BUTTON_POSITIVE},
         *            {@link DialogInterface#BUTTON_NEGATIVE}, or
         *            {@link DialogInterface#BUTTON_NEUTRAL}
         * @param text The text to display in positive button.
         * @param listener The {@link DialogInterface.OnClickListener} to use.
         */
        public void setButton(int whichButton, CharSequence text, OnClickListener listener) {
            mAlert.setButton(whichButton, text, listener, null);
        }
    

    whichButton 有三种功能类型 ,用于实现对应功能和唯一标识一个Button,以进行后续操作

        /** The identifier for the positive button. */
        int BUTTON_POSITIVE = -1;
    
        /** The identifier for the negative button. */
        int BUTTON_NEGATIVE = -2;
    
        /** The identifier for the neutral button. */
        int BUTTON_NEUTRAL = -3;
    

    添加Button时,需要传入whichButton的类型,如BUTTON_POSITIVE

            mDialog!!.setButton(BUTTON_POSITIVE,"点我重试") { dialog, which -> ToastUtils.showShortToast(mContext,"点我干嘛?") }
    

    控制Button,可根据whichButton来获取对应Button,来进行操作

           mDialog!!.getButton(BUTTON_POSITIVE).visibility = View.VISIBLE
    

    如果在子线程中,是不能直接更新UI的

        fun showNetErrorDialog(){
            (mContext as Activity).runOnUiThread {
                mDialog!!.setMessage(net_error)
                mDialog!!.getButton(BUTTON_POSITIVE).visibility = View.VISIBLE
            }
        }
    
    效果图

    相关文章

      网友评论

          本文标题:ProgressDialog 使用心得

          本文链接:https://www.haomeiwen.com/subject/ifsbyftx.html