ProgressDialog 使用心得

遇到一个需求,现在新版本的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
        }
    }
效果图
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容