本篇文章給大家分享的是有關(guān)使用Toast類怎么避免顯示時間疊加,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

具體內(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();
}
}這是一個封裝好的Toast工具類,避免時間疊加
使用方法
ToastUtil.getInstance().showToast(mContext,"test");
如果在線程中執(zhí)行的話,必須按照如下格式
ToastUtil.getInstance().showToastInThread(mContext,"str");
以上就是使用Toast類怎么避免顯示時間疊加,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享題目:使用Toast類怎么避免顯示時間疊加-創(chuàng)新互聯(lián)
分享鏈接:http://chinadenli.net/article30/dhesso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、微信小程序、標(biāo)簽優(yōu)化、Google、小程序開發(fā)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容