Linjiajia
2023-08-27 1df231fd6aafa221aef3532d069c7e27d4331be7
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.android.app_base.base.dialog;
 
import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;
 
import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;
 
import com.android.app_base.R;
 
/**
 * 提示对话框
 */
public final class TipsDialog {
 
    public final static int ICON_FINISH = R.drawable.tips_finish_ic;
    public final static int ICON_ERROR = R.drawable.tips_error_ic;
    public final static int ICON_WARNING = R.drawable.tips_warning_ic;
 
    public static final class Builder
            extends BaseDialog.Builder<Builder>
            implements Runnable, BaseDialog.OnShowListener {
 
        private final TextView mMessageView;
        private final ImageView mIconView;
 
        private int mDuration = 2000;
 
        public Builder(Context context) {
            super(context);
            setContentView(R.layout.tips_dialog);
            setAnimStyle(BaseDialog.ANIM_TOAST);
            setBackgroundDimEnabled(false);
            setCancelable(false);
 
            mMessageView = findViewById(R.id.tv_tips_message);
            mIconView = findViewById(R.id.iv_tips_icon);
 
            addOnShowListener(this);
        }
 
        public Builder setIcon(@DrawableRes int id) {
            mIconView.setImageResource(id);
            return this;
        }
 
        public Builder setDuration(int duration) {
            mDuration = duration;
            return this;
        }
 
        public Builder setMessage(@StringRes int id) {
            return setMessage(getContext().getString(id));
        }
        public Builder setMessage(CharSequence text) {
            mMessageView.setText(text);
            return this;
        }
 
        @Override
        public BaseDialog create() {
            // 如果显示的图标为空就抛出异常
            if (mIconView.getDrawable() == null) {
                throw new IllegalArgumentException("显示的图标为空");
            }
            // 如果内容为空就抛出异常
            if (TextUtils.isEmpty(mMessageView.getText().toString())) {
                throw new IllegalArgumentException("内容为空");
            }
 
            return super.create();
        }
 
        @Override
        public void onShow(BaseDialog dialog) {
            // 延迟自动关闭
            postDelayed(this, mDuration);
        }
 
        @Override
        public void run() {
            if (!isShowing()) {
                return;
            }
            dismiss();
        }
    }
}