本篇文章給大家分享的是有關(guān)使用Toast類怎么避免顯示時(shí)間疊加,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
成都創(chuàng)新互聯(lián)專注于永泰企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計(jì),商城網(wǎng)站定制開(kāi)發(fā)。永泰網(wǎng)站建設(shè)公司,為永泰等地區(qū)提供建站服務(wù)。全流程定制制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)具體內(nèi)容如下
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(); } }
這是一個(gè)封裝好的Toast工具類,避免時(shí)間疊加
使用方法
ToastUtil.getInstance().showToast(mContext,"test");
如果在線程中執(zhí)行的話,必須按照如下格式
ToastUtil.getInstance().showToastInThread(mContext,"str");
以上就是使用Toast類怎么避免顯示時(shí)間疊加,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享題目:使用Toast類怎么避免顯示時(shí)間疊加-創(chuàng)新互聯(lián)
分享鏈接:http://muchs.cn/article30/dhesso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、微信小程序、標(biāo)簽優(yōu)化、Google、小程序開(kāi)發(fā)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容