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参数,单位为毫秒
 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号