封装一个Toast的工具类,可以实现页面销毁同时Toast也会消失,解决Toast长时间显示的问题。
public class ToastUtils {
private Toast mToast;
private static ToastUtils mToastUtils;
private ToastUtils(Context context) {
mToast = Toast.makeText(context.getApplicationContext(), null, Toast.LENGTH_SHORT);
}
public static synchronized ToastUtils getInstanc(Context context) {
if (null == mToastUtils) {
mToastUtils = new ToastUtils(context);
}
return mToastUtils;
}
/**
* 显示toast
*
* @param toastMsg
*/
public void showToast(int toastMsg) {
mToast.setText(toastMsg);
mToast.show();
}
/**
* 显示toast
*
* @param toastMsg
*/
public void showToast(String toastMsg) {
mToast.setText(toastMsg);
mToast.show();
}
/**
* 取消toast,在activity的destory方法中调用
*/
public void destory() {
if (null != mToast) {
mToast.cancel();
mToast = null;
}
mToastUtils = null;
}
}
|