Toast重复显示工具类

来源:互联网 发布:路由器绑定mac 编辑:程序博客网 时间:2024/05/14 13:29

开发中经常会遇到toast多次弹出的问题。这个工具类会避免这种情况发生。

import android.content.Context;import android.widget.Toast;public class ToastUtil {    private static Toast toast = null;    public static void getShortToast(Context context,int retId){        if (toast == null) {            toast = Toast.makeText(context, retId, Toast.LENGTH_SHORT);        } else {            toast.setText(retId);            toast.setDuration(Toast.LENGTH_SHORT);        }        toast.show();    }    public static void getShortToastByString(Context context,String hint){        if (toast == null) {            toast = Toast.makeText(context, hint, Toast.LENGTH_SHORT);        } else {            toast.setText(hint);            toast.setDuration(Toast.LENGTH_SHORT);        }        toast.show();    }    public static void getLongToast(Context context,int retId){        if (toast == null) {            toast = Toast.makeText(context, retId, Toast.LENGTH_LONG);        } else {            toast.setText(retId);            toast.setDuration(Toast.LENGTH_LONG);        }        toast.show();    }    public static void getLongToastByString(Context context,String hint){        if (toast == null) {            toast = Toast.makeText(context, hint, Toast.LENGTH_LONG);        } else {            toast.setText(hint);            toast.setDuration(Toast.LENGTH_LONG);        }        toast.show();    }}
原创粉丝点击