我们这样做的时候经常登录认证使用toast提示用户输入出现错误等。。很多人都直接使用
Toast.makeText(LoginActivity.this, "请联系小区的物业管理", Toast.LENGTH_SHORT) .show();
public class CustomToast {
private static Toast mToast;
private static Handler mhandler = new Handler();
private static Runnable r = new Runnable() {
public void run() {
mToast.cancel();
};
};
public static void showToast(Context context, String text, int duration) {
mhandler.removeCallbacks(r);
if (null != mToast) {
mToast.setText(text);
} else {
mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
}
mhandler.postDelayed(r, 5000);
mToast.show();
}
public static void showToast(Context context, int strId, int duration) {
showToast(context, context.getString(strId), duration);
}
}这样就能够解决一直弹toast消息的问题了
调用方法:CustomToast.showToast(this,"要内容输出",Toast.LENGTH_SHORT);
版权声明:本文博主原创文章,博客,未经同意不得转载。
原文:http://www.cnblogs.com/lcchuguo/p/4810656.html