/**
* Created by a on 2014/5/18.
*
* Toast工具类
*
* showStaticToast(final Activity act, final String msg) 弹静态吐司,无论子线程还是主线程都能执行(里面做了一个线程判断)
* showToast(Context context, final String msg) 这个也是静态吐司,但是只能在主线程中执行
* showShortToast(Context context, String message) 短时间显示Toast
* showLongToast(Context context, String message) 长时间显示Toast
*
* @author zhengjiao
*/
public class ToastUtils {
private ToastUtils() {
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/* 是否弹toast总开关 */
public static boolean isShow = true;
public static void showStaticToast(final Activity act, final String msg) {
//获取当前线程
String nowThreadName = Thread.currentThread().getName();
//如果为主线程
if ("main".equals(nowThreadName)) {
if (isShow)
showToast(act, msg);
//如果为子线程
} else {
act.runOnUiThread(new Runnable() {
@Override
public void run() {
if (isShow)
showToast(act, msg);
}
});
}
}
private static Toast toast;
//如果在主线程弹静态吐司可以使用这个,传入上下文即可
public static void showToast(Context context, final String msg) {
if (toast == null) {
toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
}
toast.setText(msg);
if (isShow)
toast.show();
}
/**
* 短时间显示Toast
*/
public static void showShortToast(Context context, String message) {
if (isShow)
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
/**
* 长时间显示Toast
*/
public static void showLongToast(Context context, String message) {
if (isShow)
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}
* Created by a on 2014/5/18.
*
* Toast工具类
*
* showStaticToast(final Activity act, final String msg) 弹静态吐司,无论子线程还是主线程都能执行(里面做了一个线程判断)
* showToast(Context context, final String msg) 这个也是静态吐司,但是只能在主线程中执行
* showShortToast(Context context, String message) 短时间显示Toast
* showLongToast(Context context, String message) 长时间显示Toast
*
* @author zhengjiao
*/
public class ToastUtils {
private ToastUtils() {
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/* 是否弹toast总开关 */
public static boolean isShow = true;
public static void showStaticToast(final Activity act, final String msg) {
//获取当前线程
String nowThreadName = Thread.currentThread().getName();
//如果为主线程
if ("main".equals(nowThreadName)) {
if (isShow)
showToast(act, msg);
//如果为子线程
} else {
act.runOnUiThread(new Runnable() {
@Override
public void run() {
if (isShow)
showToast(act, msg);
}
});
}
}
private static Toast toast;
//如果在主线程弹静态吐司可以使用这个,传入上下文即可
public static void showToast(Context context, final String msg) {
if (toast == null) {
toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
}
toast.setText(msg);
if (isShow)
toast.show();
}
/**
* 短时间显示Toast
*/
public static void showShortToast(Context context, String message) {
if (isShow)
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
/**
* 长时间显示Toast
*/
public static void showLongToast(Context context, String message) {
if (isShow)
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}
本文介绍了一款Toast工具类的实现方法,该工具类能够根据不同线程环境展示提示信息,并提供了多种展示方式,如短时间、长时间及主线程专用等。通过简单的API调用即可实现消息提示功能。
8311

被折叠的 条评论
为什么被折叠?



