本篇文章給大家分享的是有關使用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");
如果在線程中執行的話,必須按照如下格式
ToastUtil.getInstance().showToastInThread(mContext,"str");
以上就是使用Toast類怎么避免顯示時間疊加,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。