本篇文章給大家分享的是有關使用Toast類怎么避免顯示時間疊加,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
具體內容如下
import android.app.Activity; import android.app.Fragment; import android.content.Context; import android.widget.Toast; 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");
如果在線程中執(zhí)行的話,必須按照如下格式
ToastUtil.getInstance().showToastInThread(mContext,"str");
以上就是使用Toast類怎么避免顯示時間疊加,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享題目:使用Toast類怎么避免顯示時間疊加-創(chuàng)新互聯(lián)
分享鏈接:http://www.chinadenli.net/article30/dhesso.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站制作、微信小程序、標簽優(yōu)化、Google、小程序開發(fā)、關鍵詞優(yōu)化
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容