美文网首页android 技术知识
Android中实现Toast效果Dialog自动消失

Android中实现Toast效果Dialog自动消失

作者: 追梦小乐 | 来源:发表于2017-12-27 09:25 被阅读16次

    来自同事_Smile的封装,记录学习一下

    1、核心代码
    public class ToastDialog extends Dialog {
    
        public static final int DIALOG_SHOW_TIME_SHORT = 3000;
        public static final int DIALOG_SHOW_TIME_LONG = 5000;
    
        /**
         * 显示时间,单位毫秒,默认3秒
         */
        private int showTime = DIALOG_SHOW_TIME_SHORT;
        private int toastDialogTioIconId = 0;
        private Object toastShowContent;
    
        public ToastDialog(Context context) {
            super(context, R.style.style_dialog);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setCanceledOnTouchOutside(false);
            setCancelable(false);
            setContentView(R.layout.dialog_customize_toast);
    
            initView();
        }
    
        private void initView() {
            ImageView ivToastDialogIcon = (ImageView) findViewById(R.id.iv_dialog_toast_icon);
            if (getToastDialogTioIconId() != 0) {
                ivToastDialogIcon.setImageResource(getToastDialogTioIconId());
            }
    
            TextView tvToastDialogContent = (TextView) findViewById(R.id.tv_dialog_toast_content);
            ViewUtils.loadContent(tvToastDialogContent, getToastShowContent());
        }
    
        @Override
        public void show() {
            dismissDialog();
            super.show();
        }
    
        private void dismissDialog() {
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    dismiss();
                }
            }, getShowTime());
        }
    
        @Override
        public void dismiss() {
            super.dismiss();
        }
    
        @Override
        public void onBackPressed() {
            return;
        }
    
        private int getShowTime() {
            return showTime;
        }
    
        /**
         * 设置显示时间,单位毫秒
         * @param showTime 时间
         */
        public void setShowTime(int showTime) {
            this.showTime = showTime;
        }
    
        private int getToastDialogTioIconId() {
            return toastDialogTioIconId;
        }
    
        /**
         * 设置显示图标
         * @param toastDialogTioIconId 图标ID
         */
        public void setToastDialogTioIconId(int toastDialogTioIconId) {
            this.toastDialogTioIconId = toastDialogTioIconId;
        }
    
        private Object getToastShowContent() {
            return toastShowContent;
        }
    
        /**
         * 显示内容
         * @param toastShowContent 内容
         */
        public void setToastShowContent(Object toastShowContent) {
            this.toastShowContent = toastShowContent;
        }
    }
    
    2、R.layout.dialog_customize_toast布局代码
    <?xml version="1.0" encoding="utf-8"?>
    <!--自定义Toast样式dialog弹框布局-->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="@dimen/dimen_515"
        android:layout_height="@dimen/dimen_320"
        android:background="@drawable/bg_round_white_shape"
        android:gravity="center"
        android:padding="@dimen/dimen_20"
        android:orientation="vertical">
    
        <ImageView
            android:id="@+id/iv_dialog_toast_icon"
            style="@style/style_wrap_content"
            android:contentDescription="@null"/>
    
        <TextView
            android:id="@+id/tv_dialog_toast_content"
            style="@style/style_wrap_content"
            android:layout_marginTop="@dimen/dimen_20"
            android:textSize="@dimen/text_26"
            android:textColor="@color/c_black_2"
            android:maxLines="2"/>
    
    </LinearLayout>
    
    3、 R.style.style_dialog 样式代码
     <!-- 自定义DialogActivity样式-->
        <style name="style_dialog" parent="android:style/Theme.Dialog">
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:windowFrame">@null</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowIsTranslucent">true</item>
            <item name="android:windowContentOverlay">@null</item>
            <item name="android:windowFullscreen">true</item>
            <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
            <item name="android:backgroundDimEnabled">true</item>
        </style>
    

    相关文章

      网友评论

        本文标题:Android中实现Toast效果Dialog自动消失

        本文链接:https://www.haomeiwen.com/subject/yzpygxtx.html