会自动消失的自定义Dialog

前言

会自动消失的dialog
很多时候 我们不管是在自己的app开发还是看到别人的app中 都有dialog会定时的效果
例如:打开某个页面 dialog显示几秒钟后 自动关闭 那么如何实现呢?今天在项目中 也试着
自己完成了一个以供自己加深记忆学习

废话不多说 直接上代码

//自定义一个dialog类
public class AutoDismissDialog extends Dialog {
    //handler 用来更新UI的一套机制也是消息机制
    private final Handler handler = new Handler();

    public AutoDismissDialog(@NonNull Context context) {
        this(context,0);
    }
    //在构造的时候 我们约定一个style 和一个上下文
    public AutoDismissDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (handler != ) {
            //这里用到了handler的定时器效果 延迟2秒执行dismiss();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    dismiss();
                }
            }, 2000);
        }
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

在你项目中用到的时候 直接实例化就可以使用
效果如下:
进入签到页面 弹出dialog 在显示2秒以后 自动关闭
dialog

   //积分弹框
   //我的积分弹出2秒后自动关闭
   private void integral() {        
        LayoutInflater inflater = getLayoutInflater();
        View view1 = inflater.inflate(R.layout.view_my_integral_layout, null);
        TextView title = view1.findViewById(R.id.tv_title);
        //我这里重用了同一个dialog 只是显示的内容不一样
        title.setText(R.string.my_scores);
        TextView textView = view1.findViewById(R.id.tv_integral);
        textView.setText(mIntegral + "分");
        //这里是实例化dialog 下面我会贴出自己的style
        AutoDismissDialog dialog = new AutoDismissDialog(this, R.style.DialogStyle);
        dialog.setContentView(view1);
        //这里是指dialog显示的时候 点击其他空白地方是否将dialog隐藏 根据需求自己设置
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

dialog重用

//签到天数弹框
private void signInDays(int sinInDayNum) {        
        LayoutInflater inflater = getLayoutInflater();
        View view1 = inflater.inflate(R.layout.view_my_integral_layout, null);
        TextView title = view1.findViewById(R.id.tv_title);         
        title.setText(R.string.continuously_signed);
        TextView textView = view1.findViewById(R.id.tv_integral);
        textView.setText(sinInDayNum + "天");        
        AutoDismissDialog dialog = new AutoDismissDialog(this, R.style.DialogStyle);
        dialog.setContentView(view1);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

DialogStyle

<style name="DialogStyle" parent="@android:style/Theme.Dialog">
        <!-- 边框 -->
        <item name="android:windowFrame">@null</item>
        <!-- 背景透明 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!-- 无标题 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 是否浮现在activity之上 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 半透明 -->
        <item name="android:windowIsTranslucent">false</item>
        <!-- 背景模糊 -->
        <item name="android:windowContentOverlay">@null</item>
        <!-- 允许对话框的背景变暗 -->
        <item name="android:backgroundDimEnabled">true</item>
    </style>
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

2018.0606 每天进步一点点,加油!!!

        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
            </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值