连续点击吐司多次显示解决

1、 核心思想
private Toast toast;

if (toast == null)
    toast = Toast.makeText(CityManager.this, "定位出错", Toast.LENGTH_SHORT);
toast.show();

2、 提取toast工具

在要使用时,这样调用
ToastUtil.getToast(getApplicationContext(),”定位错了”);

public class ToastUtil  {
    private static Context context = null;
    private static Toast toast = null;


    public static void getToast(Context context,StringretId){
        if (toast == null) {
            toast = Toast.makeText(context, retId, Toast.LENGTH_SHORT);
        } else {
            toast.setText(retId);
            toast.setDuration(Toast.LENGTH_SHORT);
        }


        toast.show();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38340601/article/details/82707632