ToastUtils

public class ToastUtils {

    private static String TAG="ToastUtils";
    private static Toast toast;

    /**
     *單例獲取實例
     * @param context
     * @return
     */

    private static ToastUtils toastUtils;
    private  ToastUtils(Context context){
        toast=Toast.makeText(context.getApplicationContext(),null,Toast.LENGTH_SHORT);
    }
    public static ToastUtils instance(Context context){

        if(toastUtils==null){
            synchronized (ToastUtils.class){
                if(toastUtils==null){
                    toastUtils=new ToastUtils(context);
                }
            }
        }
        return toastUtils;
    }

    //短期顯示Toast
    public void showShortToast(String msg){
        toast.setText(msg);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }

    //短期顯示Toast
    public void showShortToast(int resId){
        toast.setText(resId);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }

    //長時間顯示Toast
    public void ShowLongToast(String msg){
        toast.setText(msg);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }
    //長時間顯示Toast 
    public void ShowLongToast(int resId){
        toast.setText(resId);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }

    //取消toast
    public void cancleToast(){
        if(toast!=null){
            toast.cancel();
            toast=null;
        }
        toastUtils=null;
    }

}
本站公眾號
   歡迎關注本站公眾號,獲取更多信息