Toast类避免显示时间叠加的方法

来源:互联网 发布:监控mysql sql性能 编辑:程序博客网 时间:2024/06/05 21:11
import android.content.Context;import android.widget.Toast;/** * 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();    }}

这是一个封装好的Toast工具类,避免时间叠加

使用方法

ToastUtil.getInstance().showToast(B.this,"test");

0 0
原创粉丝点击