1.Toast属于常用的提示工具,但是系统提供的toast存在以下几个问题
(1)比如点击多次按钮,会出现多次重复的Toast提示;
(2)应用已经退出,但是toast任然在;
2.Toast有两种系统定义的显示时间长短
LENGTH_SHORT--------2秒
LENGTH_LONG---------3.5秒
也可以自定义时间的长短,用duration这个参数来表示第三个参数;
3.简单封装ToastUtils,实现基本的功能:
package com.elaine.chat; import android.content.Context; import android.widget.Toast; /** * Created by llq on 2017/7/11. */ /* 1.比如:按钮按多次,toast出现多个重复的 2.退出应用了,toast还在界面上*/ public class ToastUtils { private static Toast toast=null; private static Boolean isShow=true; private ToastUtils(){ throw new UnsupportedOperationException("u can't instantiate"); } /** * 短时间提示 * @param context * @param msg */ public static void shortShow(Context context,String msg){ if (isShow){ if (toast==null){ toast=Toast.makeText(context,msg,Toast.LENGTH_SHORT); }else { toast.setText(msg); } toast.show(); } } /** * 长时间提示 * @param context * @param msg */ public static void longShow(Context context,String msg){ if (isShow){ if (toast==null){ toast=Toast.makeText(context,msg,Toast.LENGTH_LONG); }else { toast.setText(msg); } toast.show(); } } /** * 取消Toast显示 */ public void setCancel() { if(isShow && toast != null){ toast.cancel(); } } /** * 全局控制是否显示Toast * @param isShowToast */ public static void setIsShow(boolean isShowToast){ isShow = isShowToast; } }
4.文件地址:http://download.csdn.net/download/ling1008123/9896346
5.分享一篇Toast写的很不错的文章,地址:http://www.jianshu.com/p/5c5c8ee31ddb