-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToastUtil.java
81 lines (70 loc) · 2.11 KB
/
ToastUtil.java
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
package com.example.taxidata.util;
import android.view.Gravity;
import android.widget.Toast;
import com.example.taxidata.application.TaxiApp;
public class ToastUtil {
//触发多少次Toast调用,都只会持续一次Toast显示的时长
private static Toast toast;
/**
* 短时间显示Toast【居下】
*
* @param msg 显示的内容-字符串
*/
public static void showShortToastBottom(String msg) {
if (TaxiApp.getContext() != null) {
if (toast == null) {
toast = Toast.makeText(TaxiApp.getContext(), msg, Toast.LENGTH_SHORT);
} else {
toast.setText(msg);
}
toast.show();
}
}
/**
* 短时间显示Toast【居中】
*
* @param msg 显示的内容-字符串
*/
public static void showShortToastCenter(String msg) {
if (TaxiApp.getContext() != null) {
if (toast == null) {
toast = Toast.makeText(TaxiApp.getContext(), msg, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
} else {
toast.setText(msg);
}
toast.show();
}
}
/**
* 短时间显示Toast【居上】
*
* @param msg 显示的内容-字符串
*/
public static void showShortToastTop(String msg) {
if (TaxiApp.getContext() != null) {
if (toast == null) {
toast = Toast.makeText(TaxiApp.getContext(), msg, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 0);
} else {
toast.setText(msg);
}
toast.show();
}
}
/**
* 长时间显示Toast【居下】
*
* @param msg 显示的内容-字符串
*/
public static void showLongToastBottom(String msg) {
if (TaxiApp.getContext() != null) {
if (toast == null) {
toast = Toast.makeText(TaxiApp.getContext(), msg, Toast.LENGTH_LONG);
} else {
toast.setText(msg);
}
toast.show();
}
}
}