UI-Toast

ToastUtils

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

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

public class ToastUtils {
private static Toast sToast;


private ToastUtils() {

}

public static void showToast(Context context, String text) {
showToastInner(context, text, Toast.LENGTH_SHORT);
}

public static void showToast(Context context, int stringId) {
showToastInner(context, context.getString(stringId), Toast.LENGTH_SHORT);
}


public static void showToastLong(Context context, String text) {
showToastInner(context, text, Toast.LENGTH_LONG);
}

public static void showToastLong(Context context, int stringId) {
showToastInner(context, context.getString(stringId), Toast.LENGTH_LONG);
}

private static void showToastInner(Context context, String text, int duration) {
ensureToast(context);
sToast.setText(text);
//居中显示
sToast.setGravity(Gravity.CENTER, 0, 0);
//显示时长
sToast.setDuration(duration);
sToast.show();
}


@SuppressLint("ShowToast")
private static void ensureToast(Context context) {
if (sToast != null) {
return;
}
synchronized (ToastUtils.class) {
if (sToast != null) {
return;
}
sToast = Toast.makeText(context.getApplicationContext(), " ", Toast.LENGTH_SHORT);
}
}
}

居中显示带自定义布局

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
public class Toastutil {
private static Toast odltoast;

/**
* 短暂显示Toast消息
*
* @param context
* @param message
*/
public static void showShortToast(Context context, Object message) {
if (message == null) {
message = "";
}
if (context != null) {
Toast mytoast = Toast.makeText(context.getApplicationContext(), message.toString(), Toast.LENGTH_SHORT);//用getApplicationContext防止内存溢出
mytoast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
cancelToast();
odltoast = mytoast;
mytoast.show();
}
}

//先隐藏再显示新的(效果是最终会显示最后一条toast内容)
public static void cancelToast() {
if (odltoast != null) {
odltoast.cancel();
}
}

/**
* 短暂显示Toast消息
*
* @param context
* @param message
*/
public static void showlongToast(Context context, Object message) {
if (message == null) {
message = "";
}
if (context != null) {
Toast mytoast = Toast.makeText(context.getApplicationContext(), message.toString(), Toast.LENGTH_LONG);//用getApplicationContext防止内存溢出
mytoast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
cancelToast();
odltoast = mytoast;
mytoast.show();
}
}

/**
* 居中展示,带自定义布局
* @param context
* @param message
* @param duration
*/
public static void showToastCustomView(Context context, Object message, int duration) {
if (message == null) {
message = "";
}
if (context != null) {
//使用布局加载器,将编写的toast_layout布局加载进来
View view = LayoutInflater.from(context).inflate(R.layout.toast_layout, null);
//获取TextView
TextView title = (TextView) view.findViewById(R.id.toast_tv);
//设置显示的内容
title.setText(message.toString());
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
//设置显示时间
toast.setDuration(duration);
toast.setView(view);
cancelToast();
odltoast = toast;
toast.show();
}
}
}

toast_layout.xml自定义toast的布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/shape_corner_90_color_999999">


<TextView
android:id="@+id/toast_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="一段很长的测试文字"
android:textColor="@color/text_big"
android:textSize="18sp" />
</LinearLayout>

shape_corner_90_color_999999.xml圆角背景

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#999999" />
<corners android:radius="90dp" />
</shape>

Toast问题

Toast运行在子线程问题,handler问题:Can't toast on a thread that has not called Looper.prepare()

正确写法:

1
2
3
4
5
6
7
8
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
ToastUtils.showRoundRectToast("潇湘剑雨-杨充");
Looper.loop();
}
}).start();