ToastUtil工具类代码如下:
public class ToastUtil {
public static void showToast(final Context context, final String msg) {
if (Looper.myLooper() == Looper.getMainLooper()) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
return;
}
Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
});
}
public static void showToast(final Context context, final int msgId) {
if (Looper.myLooper() == Looper.getMainLooper()) {
Toast.makeText(context, msgId, Toast.LENGTH_SHORT).show();
return;
}
Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, msgId, Toast.LENGTH_SHORT).show();
}
});
}
}