ToastUtil.java
1.9 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.xdy.util;
import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.widget.Toast;
/**
* Created by zhangzhiqiang@zjsj on 2017/10/8.
*/
public class ToastUtil {
public static short POSITION_BOTTOM = 2;
public static short POSITION_CENTER = 1;
public static int DEFAULT_DURATION = 2000;
private static Toast mToast;
private static Handler mHandler = new Handler();
private static Runnable r = new Runnable() {
public void run() {
mToast.cancel();
}
};
/**
* @param text 文字
* @param duration 持续的时间
* @param position 位置
*/
public static void showToast(Context ctx, String text, int duration, short position) {
mHandler.removeCallbacks(r);
if (mToast != null)
mToast.setText(text);
else
mToast = Toast.makeText(ctx, text, Toast.LENGTH_SHORT);
if (POSITION_BOTTOM == position) {
mToast.setGravity(Gravity.BOTTOM, 0, 0);
} else {
mToast.setGravity(Gravity.CENTER, 0, 0);
}
mHandler.postDelayed(r, duration);
mToast.show();
}
public static void showToast(Context ctx, int resId, int duration, short position) {
showToast(ctx, ctx.getString(resId), duration, position);
}
public static void showToast(Context ctx, int resId, int duration) {
showToast(ctx, ctx.getString(resId), duration, POSITION_CENTER);
}
public static void showToast(Context ctx, int resId) {
showToast(ctx, ctx.getString(resId), DEFAULT_DURATION, POSITION_CENTER);
}
public static void showToast(Context ctx, String text) {
showToast(ctx, text, DEFAULT_DURATION, POSITION_CENTER);
}
public static void showToast(Context ctx, String text, int duration) {
showToast(ctx, text, duration, POSITION_CENTER);
}
}