自定义Toast实现多次触发只显示一次toast使用改良

使用场景描述

当我们处于某个场景,例如一个按钮可以触发toast的显示,当你在多次点击按钮时,会多次触发toast的显示。而调用android原生的toast的makeText的方式所生产的toast是被加到一个队列里面,然后依次执行。这样就会出现多次点击按钮触发的toast一直会按队列出现在界面上,而且即使退出了当前界面也会一直出现在手机上,而且无法手动取消,这时的用户体验变得非常的差。这时就可以使用自定义样式的toast。

自定义toast的好处

1.多次触发也只显示一个toast,只是把toast的内容替换成最新一次触发需要显示的内容。

2.可以手动取消toast的显示(不过这个取消还是会带有一点延迟效果,不是马上就消失,而是渐渐的消失,该功能主要是通过单例来实现,一直保持只有一个toast实例,这样在调用cancel方法时就能把toast给取消掉。)

3.可以自定义toast的样式,这样就可以做到定制的效果。

使用方法

1.将自定义toast引入你们的工程中,以下是toast的源码。
public enum  CustomToast {
    INSTANCE;// 实现单例
    private Toast mToast;
    private TextView mTvToast;
    public void showToast(Context ctx, String content) {
        if (mToast == null) {
            mToast = new Toast(ctx);
            mToast.setGravity(Gravity.CENTER, 0, 0);//设置toast显示的位置,这是居中
            mToast.setDuration(Toast.LENGTH_SHORT);//设置toast显示的时长
            View _root = LayoutInflater.from(ctx).inflate(R.layout.toast_custom_common, null);//自定义样式,自定义布局文件
            mTvToast = (TextView) _root.findViewById(R.id.tvCustomToast);
            mToast.setView(_root);//设置自定义的view 
       }
        mTvToast.setText(content);//设置文本
        mToast.show();//展示toast
    }
    public void showToast(Context ctx, int stringId) {
        showToast(ctx, ctx.getString(stringId));
    }
    public void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
            mTvToast = null;
        }
    }
}

2.需要的一些资源文件

1)自定义样式的布局toast_custom_common,完全可以按照自己的需求进行布局,以下是案例中的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tvCustomToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/toast_custom_common_bg"
        android:gravity="center"
        android:minWidth="200dp"
        android:padding="15dp"
        android:textColor="@color/colorBasicWhite"
        android:textSize="17sp" />
</LinearLayout>

2)资源文件
<color name="colorBasicWhite">#ffffff</color>
<color name="colorAutoDismissToast">#88000000</color>

//@drawable/toast_custom_common_bg
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp" />
    <solid android:color="@color/colorAutoDismissToast" />
</shape>

3.在你对应的页面中直接使用toast即可
public class TestCustomToastActivity extends AppCompatActivity {
    private int count = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_custom_toast);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btnShowToast,R.id.btnCancelToast})
    public void clickEvent(View v){
        switch (v.getId()){
            case R.id.btnShowToast:
                count++;
                CustomToast.INSTANCE.showToast(this,getString(R.string.custom_toast_click_number,count));
                break;
            case R.id.btnCancelToast:
                count = 0;
                CustomToast.INSTANCE.cancelToast();//可手动取消toast 
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CustomToast.INSTANCE.cancelToast();//销毁页面时,取消掉toast
    }
}


这个命题已经有人做了,今天是做个小的调整,思路和代码如下:

Activity也好Context也罢,这么做总是需要destroy个人觉得非常麻烦

所以用Application的单例会简便很多。

CustomToast.INSTANCE.showToast(MyApplication.getInstance(),content);


以后调用不用考虑是哪个Activity,统一修改也方便

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现自定义Toast显示时间: 1. 自定义一个Toast的布局文件,例如:toast_layout.xml。 2. 在代码中加载该布局文件,并设置Toast显示时间。 ``` java LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, null); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); // 设置显示时间 final int DELAY_TIME = 2000; new Handler().postDelayed(new Runnable() { @Override public void run() { toast.cancel(); } }, DELAY_TIME); toast.show(); ``` 在上面的代码中,通过Toast的setView方法设置了自定义的布局文件,然后通过Handler定时取消Toast显示。 注意:这里设置的显示时间需要根据实际情况进行调整,DELAY_TIME为毫秒数。 3. 如果需要多次显示Toast,可以将以上代码封装成一个方法,然后在需要显示Toast的地方调用该方法即可。 ``` java public static void showCustomToast(Context context, String msg) { LayoutInflater inflater = LayoutInflater.from(context); View layout = inflater.inflate(R.layout.toast_layout, null); TextView text = (TextView) layout.findViewById(R.id.text); text.setText(msg); Toast toast = new Toast(context); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); final int DELAY_TIME = 2000; new Handler().postDelayed(new Runnable() { @Override public void run() { toast.cancel(); } }, DELAY_TIME); toast.show(); } ``` 在需要显示Toast的地方调用: ``` java showCustomToast(getApplicationContext(), "自定义Toast显示时间"); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值