有时候我们点击一个按钮出现toast但是当不小心多次点击时,toast会重复出现,这时候通过下面的ToastUtil类可以实现不小心多次点击的问题。
public class ToastUtil {
/* private Context context;
public ToastUtil(Context context) {
this.context=context;
}*/
private static Toast toast;
public static void showToast(Context context,int code,String content)
{
//code=1时Toast显示的时间长,code=0时显示的时间短。
if (toast==null)
{
if (code ==0)
toast=Toast.makeText(context,content,Toast.LENGTH_SHORT);
if (code==1)
toast=Toast.makeText(context,content,Toast.LENGTH_LONG);
}
else
{
toast.setText(content);
}
toast.show();
}
}