Toast util
16lz
2021-01-26
customer_toast_bg
<?xml version="1.0" encoding="utf-8"?>
R.layout.customer_toast
?xml version="1.0" encoding="utf-8"?>
public class CustomerToast { private static Toast mToast; private static final String TAG = "CustomerToast"; private static Toast initToast(Context context, CharSequence text, int duration) { View v = initView(context, text.toString()); Toast toast = new Toast(context.getApplicationContext()); toast.setDuration(duration); toast.setView(v); toast.setGravity(Gravity.CENTER, 0, 0); return toast; } public static void show(final Context context, final CharSequence text, final int duration) { if (Thread.currentThread() == Looper.getMainLooper().getThread()) { showInUIThread(context, text, duration); } else { new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { showInUIThread(context, text, duration); } }); } } private static void showInUIThread(Context context, CharSequence text, int duration) { if (!TextUtils.isEmpty(text)) { if (mToast == null) { LogUtil.d(TAG, "mToast == null"); mToast = initToast(context, text, duration); mToast.show(); } else { LogUtil.d(TAG, "mToast != null"); mToast.cancel(); mToast = initToast(context, text, duration); // View v = initView(context, text.toString()); // mToast.setView(v); mToast.show(); } } else { LogUtil.e(TAG, "text is empty"); } } public static void show(Context context, int textId, int duration) { String text = context.getString(textId); if (!TextUtils.isEmpty(text)) { if (mToast == null) { LogUtil.d(TAG, "mToast == null"); mToast = initToast(context, text, duration); mToast.show(); } else { LogUtil.d(TAG, "mToast != null"); mToast = initToast(context, text, duration); mToast.show(); } } else { LogUtil.e(TAG, "text is empty"); } } private static View initView(Context context, String text) { View v = LayoutInflater.from(context).inflate(R.layout.customer_toast, null); TextView textView = (TextView) v.findViewById(R.id.message); textView.setText(text); return v; }}
public class ToastUtil { public static void toastNetworkError() { toastNormal(ASApplication.getInstance(), ASApplication.getInstance().getString(R.string.network_error)); } public static void toastNormal(Context context, String content) { CustomerToast.show(context.getApplicationContext(), content, Toast.LENGTH_SHORT); } public static void toastLong(Context context, String content) { CustomerToast.show(context, content, Toast.LENGTH_LONG); } public static void toastLongNormal(Context context, String content) { CustomerToast.show(context, content, Toast.LENGTH_LONG); } public static void toastNormal(String content) { CustomerToast.show(ASApplication.getAppContext(), content, Toast.LENGTH_SHORT); } public static void toastNormal(int content) { CustomerToast.show(ASApplication.getAppContext(), content, Toast.LENGTH_SHORT); }}
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer