Toast類(lèi)避免顯示時(shí)間疊加的方法
本文為大家分享了Toast類(lèi)避免顯示時(shí)間疊加的方法,供大家參考,具體內(nèi)容如下
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.widget.Toast;
/**
* Toast工具類(lèi)
* 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工具類(lèi),避免時(shí)間疊加
使用方法
ToastUtil.getInstance().showToast(mContext,"test");
如果在線程中執(zhí)行的話,必須按照如下格式
ToastUtil.getInstance().showToastInThread(mContext,"str");
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android設(shè)置當(dāng)TextView中的文字超過(guò)TextView的容量時(shí)用省略號(hào)代替
這篇文章主要介紹了Android設(shè)置當(dāng)TextView中的文字超過(guò)TextView的容量時(shí)用省略號(hào)代替 ,需要的朋友可以參考下2017-03-03
Android 判斷屏幕開(kāi)關(guān)狀態(tài)方式總結(jié)
這篇文章主要介紹了Android 判斷屏幕開(kāi)關(guān)狀態(tài)方式總結(jié)的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android?手寫(xiě)熱修復(fù)dex實(shí)例詳解
這篇文章主要為大家介紹了Android?手寫(xiě)熱修復(fù)dex實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android中Permission權(quán)限機(jī)制的具體使用
這篇文章主要介紹了Android中Permission權(quán)限機(jī)制的具體使用,本文講解了權(quán)限級(jí)別 protection level、ICC(inter-component communication)權(quán)限保護(hù)等內(nèi)容,需要的朋友可以參考下2015-04-04
Android數(shù)據(jù)存儲(chǔ)方式操作模式解析
這篇文章主要為大家介紹了Android數(shù)據(jù)存儲(chǔ)方式操作模式解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Android入門(mén)之TableLayout應(yīng)用解析(二)
這篇文章主要介紹了Android入門(mén)之TableLayout應(yīng)用,需要的朋友可以參考下2014-08-08
Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫(huà)效果(六)
這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫(huà)效果第六篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08

