Android实现多条Toast快速显示(强制中止上一条Toast的显示)
来源:互联网 发布:手机皮卡堂软件 编辑:程序博客网 时间:2024/05/16 14:20
Android实现多条Toast快速显示
Toast多用于我们开发人员调试使用,有时候也作为给用户的弱提示使用,我们常用的方法是
Toast.makeText(this, "弹出Toast", Toast.LENGTH_SHORT).show();
那么问题来了,这样实现就会有一个问题,每一条Toast都要显示1S左右的时间,如果调试的Toast较多,能急死
实现Toast显示的时候,中止上一条Toast的显示
有时候就是有这种需求,也可以方便我们的调试
先说一下上一种方式为什么就每个都显示1s左右的时间,因为makeText就是获取一个Toast对象,然后之间show了出来,相当于一个队列,每显示一条,就创建一个对象,在后面显示出来,每个都是一秒,
那么解决方案,就是只用一个Toast对象来显示,然后使用setText方法来设置显示内容,最后show显示出来
private Toast toast = null;/** * 弹出Toast(中断正在显示的Toast) */public void showToast(View view) { if (toast == null) { toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT); } toast.setText("弹出Toast(中断正在显示的Toast)"); toast.show();}
工具类(为了方便,可以直接复制粘贴使用)
package googleanalytics.example.com.kqwtoastdemo;import android.content.Context;import android.widget.Toast;/** * Created by kongqw on 2015/9/28. */public class ToastUtile { // 构造方法私有化 不允许new对象 private ToastUtile() { } // Toast对象 private static Toast toast = null; /** * 显示Toast */ public static void showText(Context context, String text) { if (toast == null) { toast = Toast.makeText(context, "", Toast.LENGTH_SHORT); } toast.setText(text); toast.show(); }}
测试类
package googleanalytics.example.com.kqwtoastdemo;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 弹出Toast */ public void showToastDefault(View view) { Toast.makeText(this, "弹出Toast", Toast.LENGTH_SHORT).show(); } private int mShowCount = 0; /** * 弹出Toast(中断正在显示的Toast) */ public void showToast(View view) { ToastUtile.showText(this, "弹出Toast(中断正在显示的Toast) " + mShowCount++); }}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="showToastDefault" android:text="弹出Toast" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="showToast" android:text="弹出Toast(中断正在显示的Toast)" /></LinearLayout>
效果图
0 0
- Android实现多条Toast快速显示(强制中止上一条Toast的显示)
- Toast只显示最后一条
- android service组件上显示toast插件
- android service组件上显示toast插件
- 实现第一条Toast还没显示完直接显示第二条内容,改变Toast内容
- Android不同版本上Toast连续显示的问题
- Android不同版本上Toast连续显示的问题
- Android Toast显示动画
- Android Toast显示动画
- Android中用Toast.cancel()方法优化toast内容的显示
- Android中用Toast.cancel()方法优化toast内容的显示
- Android中用Toast.cancel()方法优化toast内容的显示
- Android中用Toast.cancel()方法优化toast内容的显示
- Android中用Toast.cancel()方法优化toast内容的显示
- Android自定义Toast,指定Toast信息显示的位置并使用Toast显示其他View
- android 使用Dialog实现Toast效果,解决有的机型不显示Toast的问题(保证能用)
- 自定义显示的Toast
- 居中显示的Toast
- maven install Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-wa
- 5步解决移动设备上的300ms点击延迟
- MySQL重连,连接丢失问题解决
- Mysql CPU占用高的问题解决方法小结
- MySQL表与数据向Oracle移植(使用Oracle SQL Developer工具)
- Android实现多条Toast快速显示(强制中止上一条Toast的显示)
- 【FFmpeg音视频编解码技术】开篇
- “钱三篇”后续之汇率(下)
- Android手机联系人源码(添加联系人页面一个功能)修改
- java的四舍五入与保留位
- hashtable和hashmap两点典型区别
- Go语言自己实现的异步小log程序.
- StringUtils
- android flag分析