4:Toast类封装
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了4:Toast类封装相关的知识,希望对你有一定的参考价值。
1:ToastUtil.java
import android.content.Context; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.TextView; import android.widget.Toast; import com.cqsl.pay.R; /** * Toast提示工具类 * @author baoxl * */ public class ToastUtil { private static final long TOAST_THRESHOLD = 2000; private static long previous = 0; private static Toast toast; private static Context context; private static TextView tipTv; private ToastUtil() {} public static void init(Context ctx) { context = ctx; } public static void toast(String message) { toast(message, Toast.LENGTH_LONG); } public static void toast(int id) { toast(id, Toast.LENGTH_LONG); } public static void toast(int id, int duration) { String message = context.getString(id); toast(message, duration); } public static void toast(String text, int duration) { long now = System.currentTimeMillis(); if (now - previous < TOAST_THRESHOLD) { tipTv.setText(text); toast.show(); } else { toast = new Toast(context); View view = LayoutInflater.from(context).inflate(R.layout.toast_view, null); tipTv = (TextView) view.findViewById(R.id.toast_text); tipTv.setText(text); toast.setDuration(duration); toast.setGravity(Gravity.BOTTOM, 0, 0); toast.setView(view); toast.show(); } previous = now; } public static void toast(String text, int duration, int xOffset, int yOffset) { long now = System.currentTimeMillis(); if (now - previous < TOAST_THRESHOLD) { tipTv.setText(text); toast.show(); } else { toast = new Toast(context); View view = LayoutInflater.from(context).inflate(R.layout.toast_view, null); tipTv = (TextView) view.findViewById(R.id.toast_text); tipTv.setText(text); toast.setDuration(duration); toast.setView(view); toast.setGravity(Gravity.NO_GRAVITY, xOffset, yOffset); toast.show(); } previous = now; } public static void cancel() { if (toast != null) { toast.cancel(); } } }
以上是关于4:Toast类封装的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段14——Vue的axios网络请求封装