当前位置: 首页 > news >正文

实战:Android 自定义菊花加载框(带超时自动消失) - 教程

一、前言

在 Android 项目开发中,经常需要用到 加载中菊花(类似 iOS 的 HUD 效果),例如支付请求、接口调用等场景。

二、实现效果


如果只是简单写个 ProgressDialog,可控性不强,也不够美观。

下面我分享一个封装好的 菊花加载框工具类 —— SdkLoadingDialog,支持:

✅ 自定义布局(菊花 + 提示文字)
✅ 半透明黑色圆角背景(类似 iOS HUD)
✅ 超时保护(默认 15 秒后自动消失,避免菊花转不停)
✅ 全局静态调用,简单易用

三、核心工具类:SdkLoadingDialog.java

package com.example.sdk.config;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.example.sdk.R;
public class SdkLoadingDialog extends Dialog {
private static SdkLoadingDialog instance;
private static Handler handler = new Handler(Looper.getMainLooper());
private static Runnable timeoutRunnable;
private SdkLoadingDialog(Context context, int themeResId) {
super(context, themeResId);
}
/**
* 显示菊花
* @param context Activity
* @param message 提示信息
*/
public static void show(Context context, String message) {
hide(); // 先清理旧实例,避免多次 show
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.dialog_loading, null);
TextView msgText = view.findViewById(R.id.tipTextView);
if (message != null && message.length() > 0) {
msgText.setText(message);
msgText.setVisibility(View.VISIBLE);
} else {
msgText.setVisibility(View.GONE);
}
instance = new SdkLoadingDialog(context, R.style.MyDialogStyle);
instance.setCancelable(false); // 禁止返回键取消
instance.setContentView(view);
instance.show();
// 超时保护:15 秒后强制关闭
timeoutRunnable = SdkLoadingDialog::hide;
handler.postDelayed(timeoutRunnable, 15000);
}
/**
* 隐藏菊花
*/
public static void hide() {
if (instance != null && instance.isShowing()) {
Context ctx = instance.getContext();
if (ctx != null) {
instance.dismiss();
}
}
instance = null;
if (handler != null && timeoutRunnable != null) {
handler.removeCallbacks(timeoutRunnable);
timeoutRunnable = null;
}
}
}

四、自定义布局:res/layout/dialog_loading.xml

五、背景样式:res/drawable/bg_loading_dialog.xml

六、自定义主题:res/values/styles.xml

@android:color/transparent
@null
true
true
false

七、使用方法

在需要显示加载框时调用:

// 显示菊花
SdkLoadingDialog.show(this, "正在处理支付...");
// 成功/失败/取消时关闭菊花
SdkLoadingDialog.hide();

附件:菊花图标

http://www.jsqmd.com/news/1743/

相关文章:

  • 代码中的善意:构建人性化的软件开发文化
  • 超级恶心的题面 [USACO21OPEN] Portals G
  • 如何隐藏一个元素
  • 昆仑通态触摸屏保存参数到内部存储器并读取的方法成都控制器开发提供
  • 使用reCAPTCHA提升WordPress网站安全性 - 指南
  • 软工9.22
  • 在控制台执行可列出所有placeholder样式
  • 9/22
  • LaTeX入门:10分钟掌握核心用法 - 详解
  • 对于一门古老东欧玄学的初步研究的简要报告
  • Codeforces 2127 D(图论,组合数学,DFS,分类讨论)
  • Java学习笔记:从三个实验看编程思维的锤炼
  • 完整教程:App 上架平台全解析,iOS 应用发布流程、苹果 App Store 审核步骤
  • 题解:AT_arc068_d [ARC068F] Solitaire
  • Codeforces Round 1051 (Div. 2) D1D2题解
  • 每日报告-关于本学期的计划
  • 若依前后端分离版本二次开发(一 搭建开发环境,新建模块)
  • Python开发中都遇到哪些问题,怎么解决的
  • 【废话】
  • 深入解析:基于 Kubernetes 的湖仓一体架构部署指南
  • 完整教程:真空发生器的工作原理
  • 每日博客
  • 【分布式架构实战】Spring Cloud 与 Dubbo 深度对比:从架构到实战,谁才是微服务的王者? - 详解
  • 探展打卡 Serverless,2025 云栖大会来了
  • 从 0 到 1,AI 走进服装店:记住每位顾客的喜好,比你还靠谱
  • STM32HAL 飞快入门(十九):UART 编程(二)—— 中断方式实现收发及局限分析
  • 贪心算法应用:多重背包启发式疑问详解
  • 划重点|云栖大会「AI 原生应用架构论坛」看点梳理
  • 君子如水,心中有火:vivo本心而为30周年
  • Margin 塌陷问题如何解决?触发BFC。BFC的概念和触发条件