Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Toast 封裝,避免Toast消息覆蓋,替換系統Toast最好用的封裝

Android Toast 封裝,避免Toast消息覆蓋,替換系統Toast最好用的封裝

編輯:關於Android編程

Android Toast 封裝,避免Toast消息覆蓋,無阻塞,等強大功能    
import android.content.Context;  
import android.os.Handler;  
import android.os.Looper;  
import android.widget.Toast;  
  
/** 
 * Toast工具類 
 * @author WikerYong   Email:<a href="#">[email protected]</a> 
 * @version 2012-5-21 下午9:21:01 
 */  
public class ToastUtils {  
    private static Handler handler = new Handler(Looper.getMainLooper());  
    private static Toast toast = null;  
    private static Object synObj = new Object();  
  
    /** 
     * Toast發送消息,默認Toast.LENGTH_SHORT 
     * @author WikerYong   Email:<a href="#">[email protected]</a> 
     * @version 2012-5-22 上午11:13:10 
     * @param act 
     * @param msg 
     */  
    public static void showMessage(final Context act, final String msg) {  
        showMessage(act, msg, Toast.LENGTH_SHORT);  
    }  
      
    /** 
     * Toast發送消息,默認Toast.LENGTH_LONG 
     * @author WikerYong   Email:<a href="#">[email protected]</a> 
     * @version 2012-5-22 上午11:13:10 
     * @param act 
     * @param msg 
     */  
    public static void showMessageLong(final Context act, final String msg) {  
        showMessage(act, msg, Toast.LENGTH_LONG);  
    }  
  
    /** 
     * Toast發送消息,默認Toast.LENGTH_SHORT 
     * @author WikerYong   Email:<a href="#">[email protected]</a> 
     * @version 2012-5-22 上午11:13:35 
     * @param act 
     * @param msg 
     */  
    public static void showMessage(final Context act, final int msg) {  
        showMessage(act, msg, Toast.LENGTH_SHORT);  
    }  
      
    /** 
     * Toast發送消息,默認Toast.LENGTH_LONG 
     * @author WikerYong   Email:<a href="#">[email protected]</a> 
     * @version 2012-5-22 上午11:13:35 
     * @param act 
     * @param msg 
     */  
    public static void showMessageLong(final Context act, final int msg) {  
        showMessage(act, msg, Toast.LENGTH_LONG);  
    }  
  
    /** 
     * Toast發送消息 
     * @author WikerYong   Email:<a href="#">[email protected]</a> 
     * @version 2012-5-22 上午11:14:09 
     * @param act 
     * @param msg 
     * @param len 
     */  
    public static void showMessage(final Context act, final int msg,  
            final int len) {  
        new Thread(new Runnable() {  
            public void run() {  
                handler.post(new Runnable() {  
  
                    @Override  
                    public void run() {  
                        synchronized (synObj) {  
                            if (toast != null) {  
                                toast.cancel();  
                                toast.setText(msg);  
                                toast.setDuration(len);  
                            } else {  
                                toast = Toast.makeText(act, msg, len);  
                            }  
                            toast.show();  
                        }  
                    }  
                });  
            }  
        }).start();  
    }  
      
    /** 
     * Toast發送消息 
     * @author WikerYong   Email:<a href="#">[email protected]</a> 
     * @version 2012-5-22 上午11:14:27 
     * @param act 
     * @param msg 
     * @param len 
     */  
    public static void showMessage(final Context act, final String msg,  
            final int len) {  
        new Thread(new Runnable() {  
            public void run() {  
                handler.post(new Runnable() {  
  
                    @Override  
                    public void run() {  
                        synchronized (synObj) {  
                            if (toast != null) {  
                                toast.cancel();  
                                toast.setText(msg);  
                                toast.setDuration(len);  
                            } else {  
                                toast = Toast.makeText(act, msg, len);  
                            }  
                            toast.show();  
                        }  
                    }  
                });  
            }  
        }).start();  
    }  
  
    /** 
     * 關閉當前Toast 
     * @author WikerYong   Email:<a href="#">[email protected]</a> 
     * @version 2012-5-22 上午11:14:45 
     */  
    public static void cancelCurrentToast() {  
        if (toast != null) {  
            toast.cancel();  
        }  
    }  
}  

 

 
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved