码迷,mamicode.com
首页 > 其他好文 > 详细

ToastUtils

时间:2017-11-10 18:45:09      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:获取   new   null   tag   java   toast   ati   div   length   

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;
    }

}

  

ToastUtils

标签:获取   new   null   tag   java   toast   ati   div   length   

原文地址:http://www.cnblogs.com/wangjiaghe/p/7815455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!