Skip to content

Commit cd406b8

Browse files
author
吕发强
committed
修改ToastUtil ,添加 自定义Toast View 的方法。
1 parent d8fd6f2 commit cd406b8

File tree

1 file changed

+104
-3
lines changed

1 file changed

+104
-3
lines changed

library/src/main/java/com/lvfq/library/utils/LvToastUtil.java

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.view.Gravity;
5+
import android.view.View;
56
import android.widget.Toast;
67

78
/**
@@ -22,13 +23,33 @@ public class LvToastUtil {
2223
*/
2324
private static Toast toast = null;
2425

25-
private static void showToast(final Activity context, final String message, final int gravity) {
26+
public static Toast getToast() {
27+
return toast;
28+
}
29+
30+
/**
31+
* 取消当前提示
32+
*/
33+
public static void cancel() {
34+
if (toast != null) {
35+
toast.cancel();
36+
}
37+
}
38+
39+
/**
40+
* 显示系统 Toast
41+
*
42+
* @param context 作用域
43+
* @param message 显示信息
44+
* @param gravity 显示位置
45+
*/
46+
public static void showToast(final Activity context, final String message, final int gravity, final int duration) {
2647
if (context != null) {
2748
context.runOnUiThread(new Runnable() {
2849
@Override
2950
public void run() {
3051
if (toast == null) {
31-
toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
52+
toast = Toast.makeText(context, message, duration);
3253
} else {
3354
toast.setText(message);
3455
}
@@ -41,14 +62,64 @@ public void run() {
4162
}
4263
}
4364

65+
/**
66+
* 显示自定义 Toast
67+
*
68+
* @param context 作用域
69+
* @param view 自定义视图
70+
* @param gravity 显示位置
71+
*/
72+
public static void showToast(final Activity context, final View view, final int gravity, final int duration) {
73+
if (context != null) {
74+
context.runOnUiThread(new Runnable() {
75+
@Override
76+
public void run() {
77+
if (toast == null) {
78+
toast = new Toast(context);
79+
toast.setView(view);
80+
toast.setDuration(duration);
81+
} else {
82+
toast.setView(view);
83+
}
84+
if (gravity == 0) {
85+
toast.setGravity(Gravity.CENTER, 0, 0);
86+
} else {
87+
toast.setGravity(gravity, 0, 0);
88+
}
89+
toast.show();
90+
}
91+
});
92+
}
93+
}
94+
4495
/**
4596
* normal show toast
4697
*
4798
* @param context
4899
* @param message
49100
*/
50101
public static void showToast(Activity context, String message) {
51-
showToast(context, message, 0);
102+
showToast(context, message, 0, Toast.LENGTH_SHORT);
103+
}
104+
105+
/**
106+
* normal show toast
107+
*
108+
* @param context
109+
* @param message
110+
*/
111+
public static void showToast(Activity context, int gravity, String message) {
112+
showToast(context, message, gravity, Toast.LENGTH_SHORT);
113+
}
114+
115+
/**
116+
* normal show toast
117+
*
118+
* @param context
119+
* @param message
120+
*/
121+
public static void showToast(Activity context, String message, int duration) {
122+
showToast(context, message, 0, duration);
52123
}
53124

54125
/**
@@ -61,4 +132,34 @@ public static void showToastCenter(Activity activity, String message) {
61132
showToast(activity, message, Gravity.CENTER);
62133
}
63134

135+
/**
136+
* 显示自定义 Toast
137+
*
138+
* @param activity 作用域
139+
* @param view 自定义视图
140+
*/
141+
public static void showToast(Activity activity, View view) {
142+
showToast(activity, view, 0, Toast.LENGTH_SHORT);
143+
}
144+
145+
/**
146+
* 显示自定义 Toast
147+
*
148+
* @param activity 作用域
149+
* @param view 自定义视图
150+
*/
151+
public static void showToast(Activity activity, int gravity, View view) {
152+
showToast(activity, view, gravity, Toast.LENGTH_SHORT);
153+
}
154+
155+
/**
156+
* 显示自定义 Toast
157+
*
158+
* @param activity 作用域
159+
* @param view 自定义视图
160+
*/
161+
public static void showToast(Activity activity, View view, int duration) {
162+
showToast(activity, view, 0, duration);
163+
}
164+
64165
}

0 commit comments

Comments
 (0)