国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Toast類避免顯示時間疊加的方法

2019-10-21 21:39:53
字體:
來源:轉載
供稿:網友

本文為大家分享了Toast類避免顯示時間疊加的方法,供大家參考,具體內容如下

import android.app.Activity;import android.app.Fragment;import android.content.Context;import android.widget.Toast; /** * Toast工具類 * Created by user on 2016/12/22. */ public class ToastUtil {   private static Toast toast = null;  private static ToastUtil toastUtil = null;   public ToastUtil(){}   public synchronized static ToastUtil getInstance(){    if(null == toastUtil){      toastUtil = new ToastUtil();    }     return toastUtil;  }   public void showToast(Context context, String string){     if(toast != null){      toast.cancel();    }      toast = Toast.makeText(context, string,Toast.LENGTH_SHORT);      toast.show();  }   public void showToast(Fragment fragment, String string){    showToast(fragment.getActivity(),string);  }   public void showToast(Activity activity, String string){    if(toast != null){      toast.cancel();    }    toast = Toast.makeText(activity, string,Toast.LENGTH_SHORT);    toast.show();  }   public void showToastTest(Context context){    if(toast != null){      toast.cancel();    }    toast = Toast.makeText(context, "click",Toast.LENGTH_SHORT);    toast.show();  }   public void showToastTest(Fragment fragment){    showToastTest(fragment.getActivity());  }   public void showToastTest(Activity activity){    if(toast != null){      toast.cancel();    }    toast = Toast.makeText(activity, "click",Toast.LENGTH_SHORT);    toast.show();  }}     if(null == toastUtil){      toastUtil = new ToastUtil();    }     return toastUtil;  }public void showToastInThread(Context context,String msg){    Looper.prepare();    if(toast != null){      toast.cancel();    }    toast = Toast.makeText(context,msg,Toast.LENGTH_SHORT);    toast.show();    Looper.loop();  }  public void showToast(Context context, String string){     if(toast != null){      toast.cancel();    }      toast = Toast.makeText(context, string,Toast.LENGTH_SHORT);      toast.show();  } }

這是一個封裝好的Toast工具類,避免時間疊加

使用方法

ToastUtil.getInstance().showToast(mContext,"test");

如果在線程中執行的話,必須按照如下格式

ToastUtil.getInstance().showToastInThread(mContext,"str");

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 炎陵县| 左贡县| 马山县| 九龙县| 马边| 郸城县| 阿克苏市| 西丰县| 西吉县| 和田县| 顺昌县| 道真| 乡宁县| 崇阳县| 宝兴县| 金寨县| 惠安县| 唐河县| 宁阳县| 奉贤区| 朔州市| 舟曲县| 南平市| 泸溪县| 克山县| 田阳县| 政和县| 石屏县| 徐闻县| 六盘水市| 南城县| 莱西市| 百色市| 碌曲县| 凤庆县| 灵川县| 锡林郭勒盟| 旌德县| 富顺县| 吕梁市| 泰安市|