自己用来显示Toast的
2014-09-16 14:42
369 查看
<span style="font-family: Arial, Helvetica, sans-serif;"></pre><pre name="code" class="java"></span>
android 的toast不能长时间显示,纠结了半天,搞了这么一个东东,以后显示toast就用你了...
</pre><pre name="code" class="java">import android.content.Context; import android.os.Handler; import android.widget.Toast; /** * 显示Toast */ public class MyToast { private static Toast mToast; /** * 延时线程 */ private static DelayThread delay; /** * 是否正在长显示 */ public static boolean isShow = false; private static String tempStr = null; private static Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 0x00:// 持续显示用 if (!isShow) { break; } if (tempStr != null && mToast != null) { mToast.setText(tempStr); mToast.setDuration(Toast.LENGTH_LONG); mToast.show(); break; } case 0x01:// 取消用 System.out.println("-->>cancle"); isShow = false; if (delay != null) { delay.interrupt(); } // mToast=null; tempStr = null; delay = null; break; default: break; } }; }; /** * 显示吐司 * * @param str */ public static void showToast(Context context, String str) { if (isShow) cancel(); if (mToast == null) { mToast = Toast.makeText(context, str, Toast.LENGTH_LONG); } else { mToast.setText(str); mToast.setDuration(Toast.LENGTH_LONG); } mToast.show(); } /** * 取消吐司 */ public static void cancel() { // isShow = false; if (mToast != null) { mToast.cancel(); mToast = null; } if (isShow) { handler.sendEmptyMessage(0x01); } } /** * 一直显示直到取消的吐司或者显示了其他吐司 * * @param str */ public static void showToastUntillCancle(Context context, String str) { isShow = true; tempStr = str; if (mToast == null) { mToast = Toast.makeText(context, tempStr, Toast.LENGTH_LONG); mToast.show(); } if (delay == null) { delay = new DelayThread(); delay.start(); } } /** * 延时线程,每3s判断一次 * * @author Administrator * */ static class DelayThread extends Thread { @Override public void run() { System.out.println("DelayThread start!"); while (isShow) { handler.sendEmptyMessage(0x00); try { System.out.println("DelayThread sleep 3s!"); Thread.sleep(3 * 1000); } catch (InterruptedException e) { // Thread.currentThread().interrupt(); e.printStackTrace(); } } handler.sendEmptyMessage(0x01); System.out.println("DelayThread stop!"); } } }
相关文章推荐
- Toast 不显示 及Toast原理介绍和自己实现Toast
- 自定义Toast 可以根据自己的需求 设置显示时间
- J2ME 用来处理手机游戏中长字符串显示问题,自己写的,仅作参考
- 自定义Toast 可以根据自己的需求 设置显示时间
- 如何在Eclipse PDE的Error Log View中显示自己的Log
- 可用来显示空值的时间选择控件4
- 做了一个小软件用来提醒自己注意健康
- 用AJAX技术在自己Blog上聚合并显示朋友Blog的最新文章(通过RSS)
- 可用来显示空值的时间选择控件3
- 要想学好位图的读写和显示,还是得自己亲手写一个CDIB类
- 要好好管理自己的博客,用来备忘,做计划,以便朋友监督
- 自己动手定制NUnit(一):增加函数说明显示
- 使每个链接的title显示为自己url
- 如何使IE地址栏里显示自己的小图片?
- 真笨,多行多列的显示居然困扰了自己一段时间~~~
- 关于dao得一些使用问题,以备忘....也用来纪念自己解决得第一个内存泄漏问题
- 可用来显示空值的时间选择控件2
- C# 自己写的一个类,用来将结构体或类中的数据打成"数据包",进行网络发送
- 新浪中用来显示flash的函数