解決Toast重複彈出,Toast單例模式

防止Toast多次彈出,網上看到的不錯

import android.content.Context;
import android.widget.Toast;
/**
* @create date:2013-7-16
* @class describe:解決Toast重複彈出
*/

public class ToastUtil3 {
 
 
     private static String oldMsg; 
        protected static Toast toast   = null; 
        private static long oneTime=0
        private static long twoTime=0
 
        public static void showToast(Context context, String s){     
            if(toast==null){  
                toast =Toast.makeText(context, s, Toast.LENGTH_SHORT); 
                toast.show(); 
                oneTime=System.currentTimeMillis(); 
            }else
                twoTime=System.currentTimeMillis(); 
                if(s.equals(oldMsg)){ 
                    if(twoTime-oneTime>Toast.LENGTH_SHORT){ 
                        toast.show(); 
                    } 
                }else
                    oldMsg = s; 
                    toast.setText(s); 
                    toast.show(); 
                }        
            } 
            oneTime=twoTime; 
        } 
 
 
        public static void showToast(Context context, int resId){    
            showToast(context, context.getString(resId)); 
        } 
 
 
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章