ToastUtils.java
1.61 KB
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
package com.share.mvpsdk.utils;
import android.content.Context;
import android.widget.Toast;
/**
* Created by Horrarndoo on 2017/4/5.
* <p>
* toast工具类封装
*/
public class ToastUtils {
private static Toast mToast = null;
/**
* 显示一个toast提示
*
* @param resouceId toast字符串资源id
*/
public static void showToast(int resouceId) {
showToast(ResourcesUtils.getString(resouceId));
}
/**
* 显示一个toast提示
*
* @param text toast字符串
*/
public static void showToast(String text) {
showToast(text, Toast.LENGTH_SHORT);
}
/**
* 显示一个toast提示
*
* @param text toast字符串
* @param duration toast显示时间
*/
public static void showToast(String text, int duration) {
showToast(AppUtils.getContext(), text, duration);
}
/**
* 显示一个toast提示
*
* @param context context 上下文对象
* @param text toast字符串
* @param duration toast显示时间
*/
public static void showToast(final Context context, final String text, final int duration) {
/**
* 保证运行在主线程
*/
AppUtils.runOnUIThread(new Runnable() {
@Override
public void run() {
if (mToast == null) {
mToast = Toast.makeText(context, text, duration);
} else {
mToast.setText(text);
mToast.setDuration(duration);
}
mToast.show();
}
});
}
}