a
554325746@qq.com
2019-12-25 603cb36a5123e46656b06a5deb8d7ac7ff81307f
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.basic.security.utils;
 
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
 
import com.basic.security.base.BaseApplication;
 
class MyToast {
    private final TextView textView;
    private Toast mToast;
    private boolean isShow = false;
 
    private MyToast(Context context, CharSequence text, int duration) {
        View v = LayoutInflater.from(context).inflate(com.basic.security.utils.ResolutionAdaptation.eplay_toast(), null);
        textView = v.findViewById(com.basic.security.utils.RUtils.R_id_textView1);
        textView.setText(text);
        mToast = new Toast(context);
        mToast.setDuration(duration);
        mToast.setView(v);
    }
 
    public static MyToast makeText(Context context, CharSequence text, int duration) {
        return new MyToast(context, text, duration);
    }
 
    public void setText(CharSequence text) {
        if (textView != null) {
            textView.setText(text);
        }
    }
 
    public void show() {
        if (mToast != null) {
            mToast.show();
        }
    }
 
    public void setGravity(int gravity, int xOffset, int yOffset) {
        if (mToast != null) {
            mToast.setGravity(gravity, xOffset, yOffset);
        }
    }
 
    public void setDuration(int duration) {
        if (mToast != null) {
            mToast.setDuration(duration);
        }
    }
}
 
public class ToastUtil {
    private static Toast toast;
    private static Toast toast_short;
    private static MyToast toast_down;
    private static MyToast toast_long;
 
    public static void showNormalToast(final String message) {
        View v = LayoutInflater.from(BaseApplication.getApplication()).inflate(com.basic.security.utils.ResolutionAdaptation.eplay_toast(), null);
        TextView textView = v.findViewById(com.basic.security.utils.RUtils.R_id_textView1);
        textView.setText(message);
        if (Thread.currentThread().getId() == 1) {
            if (toast == null) {
                toast = new Toast(BaseApplication.getApplication());
                toast.setView(v);
                toast.setDuration(Toast.LENGTH_LONG);
            } else {
                toast.setView(v);
            }
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } else {
            BaseApplication.getApplication().activity.runOnUiThread(() -> {
                if (toast == null) {
                    toast = new Toast(BaseApplication.getApplication());
                    toast.setView(v);
                    toast.setDuration(Toast.LENGTH_LONG);
                } else {
                    toast.setView(v);
                }
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            });
        }
    }
 
    public static void show(final String message) { // 有多次连续点击不显示问题 参照showNormalToast,
        System1.out.println("toast:" + message);
        View v = LayoutInflater.from(BaseApplication.getApplication()).inflate(com.basic.security.utils.ResolutionAdaptation.eplay_toast(), null);
        TextView textView = v.findViewById(com.basic.security.utils.RUtils.R_id_textView1);
        textView.setText(message);
        if (Thread.currentThread().getId() == 1) {
            if (toast == null) {
                toast = new Toast(BaseApplication.getApplication());
                toast.setView(v);
                toast.setDuration(Toast.LENGTH_LONG);
            } else {
                toast.setView(v);
            }
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } else {
            BaseApplication.getApplication().activity.runOnUiThread(() -> {
                if (toast == null) {
                    toast = new Toast(BaseApplication.getApplication());
                    toast.setView(v);
                    toast.setDuration(Toast.LENGTH_LONG);
                } else {
                    toast.setView(v);
                }
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            });
        }
//        if (Thread.currentThread().getId() == 1) {
//            if (toast_short == null) {
//                toast_short = MyToast.makeText(BaseApplication.getApplication().activity, message, Toast.LENGTH_SHORT);
//            } else {
//                toast_short.setText(message);
//                toast_short.setDuration(Toast.LENGTH_SHORT);
//            }
//            toast_short.setGravity(Gravity.CENTER, 0, 0);
//            toast_short.show();
//        } else {
//            BaseApplication.getApplication().activity.runOnUiThread(new Runnable() {
//                
//                public void run() {
//                    if (toast_short == null) {
//                        toast_short = MyToast.makeText(BaseApplication.getApplication().activity, message, Toast.LENGTH_SHORT);
//                        toast_short.setGravity(Gravity.CENTER, 0, 0);
//                    } else {
//                        toast_short.setText(message);
//                    }
//                    toast_short.show();
//                }
//            });
//        }
    }
 
    public static void showInDown(final String message) {
        if (Thread.currentThread().getId() == 1) {
            if (toast_down == null) {
                toast_down = MyToast.makeText(BaseApplication.getApplication().activity, message, Toast.LENGTH_SHORT);
                toast_down.setGravity(Gravity.CENTER, 0, 120);
            } else {
                toast_down.setText(message);
            }
            toast_down.show();
        } else {
            BaseApplication.getApplication().activity.runOnUiThread(() -> {
                if (toast_down == null) {
                    toast_down = MyToast.makeText(BaseApplication.getApplication().activity, message, Toast.LENGTH_SHORT);
                    toast_down.setGravity(Gravity.CENTER, 0, 120);
                } else {
                    toast_down.setText(message);
                }
                toast_down.show();
            });
        }
    }
 
    public static void showLong(final String message) {
        if (Thread.currentThread().getId() == 1) {
            if (toast_long == null) {
                toast_long = MyToast.makeText(BaseApplication.getApplication().activity, message, Toast.LENGTH_LONG);
                toast_long.setGravity(Gravity.CENTER, 0, 0);
            } else {
                toast_long.setText(message);
            }
            toast_long.show();
        } else {
            BaseApplication.getApplication().activity.runOnUiThread(() -> {
                if (toast_long == null) {
                    toast_long = MyToast.makeText(BaseApplication.getApplication().activity, message, Toast.LENGTH_LONG);
                    toast_long.setGravity(Gravity.CENTER, 0, 0);
                } else {
                    toast_long.setText(message);
                }
                toast_long.show();
            });
        }
    }
}