Android 多线程下更新 Dialog

本文详细介绍了如何在Android应用中创建和管理加载对话框。首先声明了私有的Handler、Runnable和Dialog变量,然后初始化这些组件。接着提供了显示和隐藏对话框的方法,其中showProgressDialog方法会延迟200毫秒展示对话框,而hideProgressDialog则会立即关闭它。对话框的内容可以根据传入的消息动态更新,并且不允许用户在加载过程中点击退出。
摘要由CSDN通过智能技术生成

1、声明

private Handler mHandler;
private Runnable mDelayShowDialogRunnable;
protected Dialog mProgressDialog;
private String message="加载中...";

2、初始化 

mHandler = new Handler();
initialProgressDialog();

  

3、方法调用

public synchronized void showProgressDialog(String msg) {
    if (TextUtils.isEmpty(msg)){
        message="加载中...";
    }else {
        message=msg;
    }

    showProgressDialog();
}

public synchronized boolean hideProgressDialog() {
    dismissProgressDialog();
    return true;
}

private void initialProgressDialog() {
    try{
        mDelayShowDialogRunnable = new Runnable() {
            @Override
            public void run() {

                view = LayoutInflater.from(SelectReceiverActivity3.this).inflate(R.layout.dialog_common_loading, null);
                mProgressDialog = new Dialog(SelectReceiverActivity3.this, R.style.Godinsec_Diglog);
                mProgressDialog.setContentView(view);
                mProgressDialog.setCanceledOnTouchOutside(false);
                tv_Content = view.findViewById(R.id.tv_Content);
                tv_Content.setText(message);
                mProgressDialog.show();
                
            }
        };
    }catch (Exception e){
    }
}

protected synchronized void showProgressDialog() {
    if (null != mProgressDialog && mProgressDialog.isShowing()) {
        tv_Content.setText(message);
        return;
    }
    dismissProgressDialog();
    mHandler.postDelayed(mDelayShowDialogRunnable, 200);

}

private synchronized void dismissProgressDialog() {
    mHandler.removeCallbacks(mDelayShowDialogRunnable);
    if (null != mProgressDialog) {
        mProgressDialog.dismiss();
        mProgressDialog = null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值