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>

效果图

更多相关文章

  1. 学习Google的战略布局...
  2. Android(安卓)从setContentView谈Activity界面的加载过程
  3. Android(安卓)Studio 制作简单的App欢迎页面——基于Android(安
  4. Android(安卓)动态加载布局
  5. Android进程管理机制和内存机制
  6. Android(安卓)秒开,对黑白屏说 再见bye bye
  7. Android(安卓)UI布局优化之ViewStub
  8. Android(安卓)中性能优化之布局优化
  9. Android(安卓)仿知乎创意广告 广告还能这么玩?

随机推荐

  1. 软件测试面试笔试题
  2. android sensors
  3. Android 获取控件的宽和高
  4. android编译错误
  5. Android(安卓)滑动scrollBy()和scrollTo(
  6. Android Tab 控件详解及实例
  7. android 知识点汇总 这一篇就够了
  8. 64位Ubuntu Kylin14编译android内核
  9. 为android-support-v4.jar打包源码
  10. Samsung 公佈可升級 Android(安卓)4.0 的