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

首頁(yè) > 系統(tǒng) > Android > 正文

Toast類避免顯示時(shí)間疊加的方法

2019-12-12 00:33:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

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

這是一個(gè)封裝好的Toast工具類,避免時(shí)間疊加

使用方法

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

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

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 曲水县| 疏附县| 洪泽县| 武威市| 勃利县| 缙云县| 土默特右旗| 台东县| 桑日县| 巴马| 邮箱| 湖北省| 冷水江市| 洪江市| 宁都县| 镶黄旗| 梓潼县| 响水县| 永州市| 大同县| 小金县| 禄劝| 德惠市| 安庆市| 郴州市| 金堂县| 临沧市| 个旧市| 微山县| 弥勒县| 镇赉县| 清新县| 大化| 易门县| 宜都市| 格尔木市| 清流县| 西乌| 灵宝市| 云霄县| 寿宁县|