单纯文字:
/**
* 防止多次点击,Toast重复显示,
*/
public class ToastUtil {
private static Toast toast;
public static void showToast(String content) {
if (toast == null) {
toast = Toast.makeText(DchApplication.getContext(), content, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
} else {
toast.setText(content);
}
toast.show();
}
}
支持图文:
public class MyToastUtil {
private static Toast toast;
/**
* 纯文字
*/
public static void showToast(String content, boolean isLengthLong) {
if (toast != null) {
toast.cancel();
}
toast = Toast.makeText(MyApplication.getInstance(),
content,
isLengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
/**
* 图文
*/
public static void showImgToast(int imgId, String content, boolean isLengthLong) {
View layout = LayoutInflater.from(MyApplication.getInstance())
.inflate(R.layout.layout_image_toast, null);
TextView text = (TextView) layout.findViewById(R.id.tv_toast);
ImageView image = (ImageView) layout.findViewById(R.id.iv_toast);
image.setImageResource(imgId);
text.setText(content);
if (toast != null) {
toast.cancel();
}
toast = new Toast(MyApplication.getInstance());
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(isLengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
}
}
子线程toast出错
参考:
原因:
Handler不能再子线程里运行的 因为子线程没有创建Looper.prepare();
主线程不需要调用,是因为主线程已经默认调用了。
解决办法:
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("currentThread1====" + Thread.currentThread());//子线程
//方式一:子线程中,looper对象需要自己给构建出来。
Looper.prepare();
System.out.println("currentThread2====" + Thread.currentThread());//子线程
Toast.makeText(MainActivity.this, "子线程显示1", Toast.LENGTH_SHORT).show();
Looper.loop();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("currentThread3====" + Thread.currentThread());//子线程
//方式二:回到主线程
runOnUiThread(new Runnable() {
@Override
public void run() {
System.out.println("currentThread4====" + Thread.currentThread());//主线程
Toast.makeText(MainActivity.this, "子线程显示2", Toast.LENGTH_SHORT).show();
}
});
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("currentThread5====" + Thread.currentThread());//子线程
//方式三:获取主线程的looper
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
System.out.println("currentThread6====" + Thread.currentThread());//主线程
Toast.makeText(MainActivity.this, "子线程显示3", Toast.LENGTH_SHORT).show();
}
});
}
}).start();
工具类:
public class ToastUtil {
private static Toast toast;
public static void show(Activity activity, String content) {
if (isInMainThread()) {//主线程显示toast
showToast(activity, content);
} else {//子线程显示toast
Looper.prepare();
showToast(activity, content);
Looper.loop();
}
}
private static void showToast(Activity activity, String content) {
if (toast == null) {
toast = Toast.makeText(activity, content, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
} else {
toast.setText(content);
}
toast.show();
}
/*----------------------------判断是否主线程方法一----------------------------*/
public static boolean isInMainThread() {
return Looper.myLooper() == Looper.getMainLooper();
}
/*----------------------------判断是否主线程方法二----------------------------*/
public static boolean isMainThread() {
return Thread.currentThread() == Looper.getMainLooper().getThread();
}
}