【Android组件】封装加载弹框

📖封装加载弹框

    • [✅1. 构造LoadingDialog](#✅1. 构造LoadingDialog)
    • [✅2. 调用LoadingDialog](#✅2. 调用LoadingDialog)

效果:

✅1. 构造LoadingDialog

构造LoadingDialog类涉及到设计模式中的建造者模式,进行链式调用,注重的是构建的过程,设置需要的属性。

步骤一:在utils包下创建LoadingDialog

java 复制代码
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class LoadingDialog extends Dialog {

    public LoadingDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private Context context;//上下文
        private String message;//提示信息
        private boolean isShowMessage = true;//是否显示提示信息
        private boolean isCancelable = false;//返回键是否可以取消
        private boolean isCancelOutside = false;//点击外部是否可以取消

        //构造方法传入上下文
        public Builder(Context context) {
            this.context = context;
        }

        //设置提示信息
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        //设置是否显示提示信息
        public Builder setShowMessage(boolean isShowMessage) {
            this.isShowMessage = isShowMessage;
            return this;
        }

        //设置是否可以按返回键取消
        public Builder setCancelable(boolean isCancelable) {
            this.isCancelable = isCancelable;
            return this;
        }

        //设置是否可以取消
        public Builder setCancelOutside(boolean isCancelOutside) {
            this.isCancelOutside = isCancelOutside;
            return this;
        }

        //创建LoadingDialog对象
        public LoadingDialog create() {
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.dialog_loading, null);
            LoadingDialog loadingDailog = new LoadingDialog(context, R.style.MyProgressDialog);
            TextView msgText = (TextView) view.findViewById(R.id.messageTextView);
            if (isShowMessage) {
                msgText.setText(message);
            } else {
                msgText.setVisibility(View.GONE);
            }
            loadingDailog.setContentView(view);
            loadingDailog.setCancelable(isCancelable);
            loadingDailog.setCanceledOnTouchOutside(isCancelOutside);
            return loadingDailog;
        }
        
    }
}

步骤二:在layout文件下添加组件:dialog_loading.xml

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:gravity="center">
	<!--白色:<color name="white">#ffffffff</color>
    -->

	<LinearLayout
		android:layout_width="140dp"
		android:layout_height="100dp"
		android:gravity="center"
		android:orientation="vertical"
		android:background="@drawable/shape_dialog_redius_gray"
		>
		<ProgressBar
			android:id="@+id/progressBar"
			android:layout_width="40dp"
			android:layout_height="40dp"
			android:layout_centerHorizontal="true"
			android:layout_centerVertical="true"
			android:indeterminate="true"
			android:indeterminateTint="@android:color/white" />

		<TextView
			android:id="@+id/messageTextView"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_below="@id/progressBar"
			android:layout_centerHorizontal="true"
			android:layout_marginTop="16dp"
			android:textColor="@android:color/white"
			android:textSize="16sp" />
	</LinearLayout>

</RelativeLayout>
	

步骤三:在drawable文件下添加shape:shape_dialog_redius_gray.xml

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!--黑色半透明:<color name="black_transparent">#98000000</color>
    -->
    <solid android:color="@color/black_transparent" />
    <corners android:radius="5dp" />
</shape>

步骤四:在 values 文件下的themes.xml下添加如下主题

xml 复制代码
	<!--弹框加载样式-->
    <!--透明色:<color name="transparent">#00000000</color>-->
    <style name="MyProgressDialog" parent="Theme.AppCompat.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item><!--背景透明-->
        <item name="android:windowIsFloating">true</item><!--是否浮动-->
        <item name="android:backgroundDimEnabled">false</item><!--对话框背后的内容是否被暗淡-->
        <item name="android:windowContentOverlay">@null</item><!--设置窗口的内容覆盖物-->
        <item name="android:statusBarColor">@null</item><!--状态栏背景色-->
    </style>

✅2. 调用LoadingDialog

在点击事件或者发生http请求时显示弹框,请求结束后关闭显示即可,下面是使用1秒延时来模拟发送请求

java 复制代码
private Handler mHandler = new Handler();//全局定义

send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //加载弹窗
                LoadingDialog loadingDialog = new LoadingDialog.Builder(getActivity())
                        .setMessage("加载中...")
                        .setCancelable(true)//返回键是否可关闭
                        .setCancelOutside(false)//点击弹框外是否可关闭
                        .create();
                
                //显示
                loadingDialog.show();

                //模拟异步发送请求后关闭加载弹窗
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //关闭显示
                        loadingDialog.dismiss();
                    }
                }, 1000);
            }
        });
相关推荐
私人珍藏库1 小时前
【Android】Soul v5.86.0 内置模块版
android·app·工具·软件·多功能
千里马学框架2 小时前
aosp新增窗口层级 Type 完整实现方案(有源码)-wms需求和面试题
android·智能手机·架构·wms·aaos·车机
峥嵘life7 小时前
Android 蓝牙设备连接广播详解-2026
android·python·学习
MusingByte10 小时前
别再裸用 Claude Code 了!安卓开发者必装 13 个官方推荐插件,效率翻 3 倍省 70% token
android
_李小白10 小时前
【android opencv学习笔记】Day 29: 滤波算法之Sobel 边缘检测
android·opencv·学习
Dxy123931021611 小时前
Python 操作 MySQL 事务:从入门到避坑
android·python·mysql
峥嵘life13 小时前
Android getprop 属性限制详解:User 版本属性获取问题分析
android·开发语言·python·学习
一航jason14 小时前
Speed Tools:一套低侵入的 Android 插件化 + 动态换肤 + 字体切换框架
android·插件化·组件化·换肤
李斯维15 小时前
Jetpack 可观察数据容器 LiveData 的入门与基础使用
android·android jetpack
问心无愧051315 小时前
ctf show web入门261
android·前端·笔记