随便写了个自定义dialog 可以读秒自动关闭 看一下就知道了 很简单


/*** Created by miao on 2016/12/6.* 所有的上下文对象必须以activity.this这种传入  否则找不到依赖的窗体 */
public class CustomAlertDialog extends AlertDialog {private static CustomAlertDialog mCustomAlertDialog;private static TextView mDialogContent;private static TextView mDialogTitle;private static TextView mCancelBtn;private static Timer mTimer;private static TimerTask mTimerTask;private static TextView mDialogUnit;private static TextView mDialogTimer;private static LinearLayout mLayout;private int count = 3;//读秒出实际开始为2-0public CustomAlertDialog(Context context) {super(context);}public CustomAlertDialog(Context context, int theme) {super(context, theme);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);LayoutInflater inflater = LayoutInflater.from(getContext());// 得到加载viewView v = inflater.inflate(R.layout.custom_alertdialog, null);// 加载布局mLayout = (LinearLayout) v.findViewById(R.id.ll_alert_dialog_match);mDialogTitle = (TextView) v.findViewById(R.id.dialog_title);mDialogContent = (TextView) v.findViewById(R.id.dialog_content);mDialogTimer = (TextView) v.findViewById(R.id.tv_timetrask);mDialogUnit = (TextView) v.findViewById(R.id.tv_unit);mCancelBtn = (TextView) v.findViewById(R.id.btn_cancel);//设置dialog圆角getWindow().setBackgroundDrawable(new BitmapDrawable());setContentView(mLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));//读秒设置dialogCountdown();}/*** 有按钮 不显示时长 点击按钮关闭 并处理其他逻辑 外部点击是否可以关闭* 显示自定义dialog*  @param context  * @param title* @param msg* @param isAllowOutsideClick*/public static void showDialog(Context context, String title, String msg, String btn_name,final OnDialogListener onDialogListener, boolean isAllowOutsideClick) {//创建自定义dialogmCustomAlertDialog = new CustomAlertDialog(context, R.style.dialogBoxTheme);mCustomAlertDialog.setCancelable(false);// 不可以用“返回键”取消mCustomAlertDialog.show();//判断是否需要按钮mCancelBtn.setVisibility(View.INVISIBLE);//是否设置外部点击if (isAllowOutsideClick){mLayout.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {closeDialogAndTimer();}});}mDialogTitle.setText(title);mDialogContent.setText(msg);mCancelBtn.setText(btn_name);mCancelBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {closeDialogAndTimer();if (onDialogListener != null) {onDialogListener.outSideExtraClick();}}});}/*** 有按钮 不显示时长 点击按钮关闭 不处理其他逻辑* 显示自定义dialog** @param context* @param title* @param msg*/public static void showDialog(Context context, String title, String msg, String btn_name) {//创建自定义dialogmCustomAlertDialog = new CustomAlertDialog(context, R.style.dialogBoxTheme);mCustomAlertDialog.setCancelable(false);// 不可以用“返回键”取消mCustomAlertDialog.show();//判断是否需要按钮if (StringUtils.isEmpty(btn_name)) {mCancelBtn.setVisibility(View.INVISIBLE);mLayout.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {closeDialogAndTimer();}});}mDialogTitle.setText(title);mDialogContent.setText(msg);mCancelBtn.setText(btn_name);mCancelBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {closeDialogAndTimer();}});}/*** 无按钮显示时长** @param context 上下文* @param title   标题* @param msg     内容* @param second  显示时长*/public static void showDialog(Context context, String title, String msg, long second) {//创建自定义dialogmCustomAlertDialog = new CustomAlertDialog(context, R.style.dialogBoxTheme);mCustomAlertDialog.setCancelable(false);// 不可以用“返回键”取消mCustomAlertDialog.show();mDialogUnit.setVisibility(View.VISIBLE);mDialogTimer.setVisibility(View.VISIBLE);//开始一个读秒定时任务mTimer.schedule(mTimerTask, 0, second);mDialogTitle.setText(title);mDialogContent.setText(msg);}/*** 关闭dialog和定时器*/private static void closeDialogAndTimer() {mCustomAlertDialog.dismiss();mTimerTask.cancel();mTimer.cancel();mTimer = null;}/*** 定义mCancelBtn的点击事件*/public interface OnDialogListener {void outSideExtraClick();}/*** 提示框的读秒方法*/private void dialogCountdown() {mTimer = new Timer();mTimerTask = new TimerTask() {@Overridepublic void run() {if (count <= 0) {UIUtils.runInMainThread(new Runnable() {@Overridepublic void run() {closeDialogAndTimer();}});}UIUtils.runInMainThread(new Runnable() {@Overridepublic void run() {mDialogTimer.setText("" + count);}});count--;}};}/*** 手动关闭方法* 关闭diaolog*/public static void closeDialog() {if (mCustomAlertDialog != null && mCustomAlertDialog.isShowing()) {mCustomAlertDialog.dismiss();mCustomAlertDialog = null;}}
}

自定义dialog 可以读秒自动关闭相关推荐

  1. android 获取对话框对象,Android 基本Dialog和自定义Dialog

    Android 基本Dialog和自定义Dialog Dialog类是对话框的基类,但你应该避免直接实例化Dialog ,可以使用子类 1.AlertDialog 此对话框可以显示标题,最多三个按钮, ...

  2. Flutter:Dialog对话框及自定义Dialog

    基本属性介绍 几个自带的dialog用法和属性基本一致,只是部分参数不同 const AlertDialog({Key? key,this.title, //对话框标题组件this.titlePadd ...

  3. android 自定义带输入框的dialog,Android 基本Dialog和自定义Dialog

    Android 基本Dialog和自定义Dialog Dialog类是对话框的基类,但你应该避免直接实例化Dialog ,可以使用子类 1.AlertDialog 此对话框可以显示标题,最多三个按钮, ...

  4. Android 自定义Dialog 的使用

    单个Dialog 自定义布局 ,这个只能算半自定义,就是一个弹框布局插入到AlertDialog 中 使用setView 即可 代码如下 AlertDialog.Builder builder = n ...

  5. Android中自定义Dialog外形,去除黑底和白色边框

    在做Android开发中经常会使用到自定义样式的Dialog,尤其是在游戏当中,大家通常都是通过自定义一个布局文件来设置Dialog中显示的内容,但是仅仅这样还是不行的~会有黑色的框和白色的边.这就需 ...

  6. android 自定义dialog 定时关闭,Android编程实现自定义Dialog的大小自动控制方法示例...

    本文实例讲述了Android编程实现自定义Dialog的大小自动控制方法.分享给大家供大家参考,具体如下: Android应用开发中,无论是出于功能还是增加用户体验,弹出对话框(Dialog)进行一些 ...

  7. 自定义Dialog(一)

    自定义Dialog(一) Dialog是一种提示用户做决定的小窗口,它一般不会布满整个屏幕,它可以请求用户在程序处理之前做一动作.比如:当退出应用的时候,可以提示用户是否真的需要退出应用,等等. an ...

  8. Android开发之自定义dialog的实现(源代码分享)

    使用系统自带的dialog如果不能满足我们日常开发的需求,那就得自己构建custom dialog,特别是对于一个app来说,统一的样式风格会给人一种舒服的感觉,所以dialog的样式 .色调一般都要 ...

  9. Android 自定义Dialog背景透明及显示位置设置

    先贴一下显示效果图,仅作参考: 代码如下: 1.自定义Dialog public class SelectDialog extends AlertDialog{public SelectDialog( ...

最新文章

  1. 有关自动目视解译系统的假设
  2. Linux 上扩展swap分区
  3. SpringBoot 2.1.3配置log4j2日志框架完整代码示例
  4. 【Linux】一步一步学Linux——cp命令(31)
  5. js控制图像等比例缩放
  6. 【计算机网络复习】1.2.2 OSI参考模型
  7. 干程序员辣么久,你为什么还没有年薪60万?
  8. Zeu.js 1.3.1 发布, 分布式系统可视化
  9. custom transition animation between UIViewControllers
  10. 路由器修改hosts实现域名劫持
  11. C语言小游戏(一)----猜数游戏
  12. 网站建设--报刊订阅管理系统(java+html)(课程设计)内置部分代码和截图,资源库中有该资源
  13. Python迷宫游戏
  14. Word文档中怎么删除空白页?删除空白页的五种方法
  15. 微信小程序新闻列表详情页
  16. c语言堆、栈、数据段、代码段、bss段的疑惑
  17. python爬虫 构建自己的代理IP池
  18. UDS之浅谈10服务
  19. lammps教程:平均值输出fix ave/time命令详解
  20. Camera驱动基础--硬件接口相关知识介绍

热门文章

  1. Python之Flask框架(一)
  2. 程序员、大数据、算法黑科技
  3. rust石头高墙要维护吗_那些害死Haskell的,也会害死Rust
  4. 003问题:约瑟夫问题(筛法解决)
  5. 002 Ubuntu系统设置之调整界面大小
  6. 我爱Flask之url_for()方法和HTTP请求
  7. Huo-deng-M
  8. Android驱动-霍尔摁键hall_sensor的实现
  9. Rmd输出pdf报错
  10. lamdba表达式的使用