如何将Toast的显示时间随意设置

前言

Android里边的Toast控件是应用与用户交互的窗口,用于提示或者警告用户一些交互的信息,但是我们会发现就算设置了Toast.LENGTH_LONG,也只有那么几秒钟时间,那么如何将这一时间随意设置呢?!

方法

首先我们写一方法:

public void showMyToast(final Toast toast, final int cnt) {
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        }, 0, 3500);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                toast.cancel();
                timer.cancel();
            }
        }, cnt );
    }

当然我们也可以将此方法设为static,加入到工具类中,方便调用。
接下来使用方法:

Toast toast=Toast.makeText(RegistActivity.this, "这是可以随意设置时间的Toast", Toast.LENGTH_LONG);
showMyToast(toast, 9*1000);

需要注意的地方

  1. 创建Toast对象的时候,要设置Toast.LENGTH_LONG,最后不要调用show()方法
  2. showMyToast()方法,传入两个参数,第一个参数为我们创建的Toast对象,第二个参数为我们想要设置显示的毫秒数

这个方法呢,算是比较巧妙的,也有投机取巧的成分!
因为Android系统Toast的显示,是存在一个Toast队列的,当一个Toast显示之后,它会从队列中取出来下一个来显示
还有在源码中Toast.LENGTH_LONG显示时间有3500毫秒,LENGTH_SHORT显示时间为2000毫秒,这个我们可以在源码中找到:
frameworks\base\services\Java\com\android\server路径下有个NotificationManagerService.java的文件:

private static final int LONG_DELAY = 3500; 
private static final int SHORT_DELAY = 2000;

我们这里采用的方法,使用一个Timer去调度一个TimeTask,delay设置的是0s,所以立即执行toast.show(),然后每隔3.5s就又执行一遍,依次类推!
最后,使用另一个Timer去调度取消之前的timer和撤销toast,而这里设置了延迟cnt毫秒执行,这里的cnt就是我们想要展示的毫秒数!

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以通过以下步骤来实现自定义 Toast 并支持设置消失时间: 1. 在 Vue 项目中创建一个自定义的 Toast 组件,可以参考下面的代码: ```vue <template> <div class="toast" v-show="show" :class="type"> {{ message }} </div> </template> <script> export default { name: 'Toast', props: { message: String, type: { type: String, default: 'info', }, time: { type: Number, default: 3000, }, }, data() { return { show: false, }; }, watch: { message() { this.show = true; setTimeout(() => { this.show = false; }, this.time); }, }, }; </script> <style scoped> .toast { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 999; padding: 10px 20px; border-radius: 5px; color: #fff; font-size: 14px; text-align: center; transition: opacity 0.3s ease-in-out; } .toast.info { background-color: #2196f3; } .toast.success { background-color: #4caf50; } .toast.warning { background-color: #ff9800; } .toast.error { background-color: #f44336; } </style> ``` 2. 在需要使用的组件中引入自定义的 Toast 组件,并在该组件中定义一个方法来显示 Toast,例如: ```vue <template> <div> <button @click="showToast">显示 Toast</button> <Toast :message="toastMessage" :type="toastType" :time="toastTime" /> </div> </template> <script> import Toast from '@/components/Toast.vue'; export default { name: 'MyComponent', components: { Toast, }, data() { return { toastMessage: '', toastType: 'info', toastTime: 3000, }; }, methods: { showToast() { this.toastMessage = 'Hello, World!'; this.toastType = 'info'; this.toastTime = 3000; }, }, }; </script> ``` 在上面的代码中,我们定义了一个 `showToast` 方法来显示 Toast,该方法会设置 `toastMessage`、`toastType` 和 `toastTime` 三个属性的值,并将这些值传递给 Toast 组件进行显示。`toastTime` 属性用来指定 Toast 消失的时间,默认为 3000 毫秒。 当 `showToast` 方法被调用后,它会设置 `toastMessage`、`toastType` 和 `toastTime` 三个属性的值,并触发这些属性的 watch 函数。在 watch 函数中,我们设置 `show` 属性为 true,表示显示 Toast,同时使用 setTimeout 函数来在指定的时间后将 `show` 属性设置为 false,从而让 Toast 消失。 以上就是一个简单的自定义 Toast 的实现方式,你可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值