Android中常用的简单ToastUtil.

Android常常用到Toast,但是没错要写很多,我只想写一些要展示的信息,其他的懒得写。

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
**
* Created by Kevin on 2017/2/17.
* Blog:http://blog.csdn.net/student9128
* Description: the utils for toast.
*/

public class ToastUtils {

private static Toast mShortToast;
private static Toast mLongToast;

public static void showToast(Context context, String message) {
if (mShortToast == null) {
mShortToast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
}
mShortToast.setText(message);
mShortToast.show();

}

public static void showToast(String message, Context context) {
if (mShortToast == null) {
mShortToast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
}
mShortToast.setText(message);
mShortToast.show();

}

public static void showLongToast(Context context, String message) {
if (mLongToast == null) {
mLongToast = Toast.makeText(context, message, Toast.LENGTH_LONG);
}
mLongToast.setText(message);
mLongToast.show();
}

public static void showLongToast(String message, Context context) {
if (mLongToast == null) {
mLongToast = Toast.makeText(context, message, Toast.LENGTH_LONG);
}
mLongToast.setText(message);
mLongToast.show();
}
}

但是这样没错还要写context,我也很烦。于是就写到了Base里面。

1
2
3
4
5
6
7
public void showToast(String message) {
ToastUtils.showToast(this, message);
}

public void showLongToast(String message) {
ToastUtils.showLongToast(this, message);
}