连续弹出的多个Toast长时间显示,不消失。
来源:互联网 发布:华西炒股软件下载 编辑:程序博客网 时间:2024/05/15 12:50
最近我在项目中自定义了Edittext,当字数到达限定值的时候,将弹Toast提示字数超限。在字数达到限定值后,再连续的输入,Toast会长时间显示不消失。通过查看源码,每次Toast.makeText(context, message, Toast.LENGTH_SHORT).show()时候,都会new一个Toast并把它加入到消息队列中排队显示,造成Toast长时间不消失,影响用户体验。
解决方案:定义ToastManager,全局使用一个Toast,这样Toast会正常消失。代码如下:
public class ToastManager {
private Toast mToast;// 如果定义成静态的变量,会造成内存泄露。
public ToastManager(Context context) {
mToast = new Toast(context.getApplicationContext());
LayoutInflater inflate = (LayoutInflater)
context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(R.layout.toast, null);
mToast.setView(v);
mToast.setDuration(Toast.LENGTH_SHORT);
}
/**
* @param message Toast的信息
*/
public void showToast(String message) {
if (null != mToast) {
mToast.setText(message);
mToast.show();
}
}
/**
* @param stringId Toast信息的id
*/
public void showToast(int stringId) {
if (null != mToast) {
mToast.setText(NoteAppImpl.getContext().getResources().getString(stringId));
mToast.show();
}
}
public void destroy() {
mToast = null;
}
}
toast.xml
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/toast_frame" android:orientation="vertical"><TextView android:id="@android:id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:fontFamily="sans-serif-condensed" android:shadowColor="#BB000000" android:shadowRadius="2.75" android:textColor="@android:color/background_light"/></LinearLayout>
- 连续弹出的多个Toast长时间显示,不消失。
- 解决吐司连续弹出,长时间不消失(简洁)
- 解决吐司连续弹出,长时间不消失(简洁)
- Android中多次弹出相同Toast提示框长时间不消失
- 避免连续点击吐司长时间不消失
- Toast连续触发 长时间显示解决办法
- Toast连续弹出的问题
- 解决用户多次点击弹出Toast队列长时间显示问题
- Toast的正确打开方式——Toast多次显示不消失——toast显示问题
- ionic中的ToastController小弹窗用法。提示信息。toast长时间不消失解决方案
- 自定义View----不消失的Toast
- 避免Toast连续弹出
- 连续二次按返回退出应用程序(多次按Toast一直长时间显示问题处理)
- Toast连续点击出现多个的解决方法
- Toast 不显示的问题
- Toast不显示的原因
- 多次点击按钮弹出多个Toast的解决办法
- 防止Toast连续显示
- PE文件是什么?
- 安卓webView实现长按二维码的自动识别功能
- 类声明和定义中的共有和私有类型的一些注意事项。
- UVA12186 Another Crisis dp 题解
- POJ 1573 Robot Motion 水模拟
- 连续弹出的多个Toast长时间显示,不消失。
- DFS hdu1181(变形课)
- SQL语句学习笔记
- Solr配置
- ImageLoder异步加载图片工具包
- Java常用类(String,File)
- 内存日志系统
- HDU 1010 Tempter of the Bone(逃离地狱,DFS,剪枝,错题集)
- 一元多项式