Android开发,Toast重复显示(显示时间过长)解决方法,另可自定义时间

 1 public class CustomToast {
 2 
 3     private static Toast mToast;
 4     private static Handler mHandler = new Handler();
 5     private static Runnable r = new Runnable() {
 6         public void run() {
 7             mToast.cancel();
 8         }
 9     };
10 
11     public static void showToast(Context mContext, String text, int duration) {
12 
13         mHandler.removeCallbacks(r);
14         if (mToast != null)
15             mToast.setText(text);
16         else
17             mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
18         mHandler.postDelayed(r, duration);
19 
20         mToast.show();
21     }
22 
23     public static void showToast(Context mContext, int resId, int duration) {
24         showToast(mContext, mContext.getResources().getString(resId), duration);
25     }
26 }

代码中的duration参数,单位为毫秒

posted @ 2014-04-28 12:21  花开花落云卷云舒  阅读(431)  评论(0)    收藏  举报