Android使用协程实现自定义Toast

news2024/10/23 5:25:13

Android使用协程实现自定义Toast

​ 最近有个消息提示需要显示10s,刚开始使用协程写了一个shoowToast方法,传入消息内容、显示时间和toast显示类型即可,以为能满足需求,结果测试说只有5s,查看日志和源码发现Android系统中Toast显示有2种类型Toast.LENGTH_SHORTToast.LENGTH_LONG,分别代表Toast消息显示的时间为短暂(大约2秒)和长时间(大约3.5秒),这和我们所需要的还是有很大差距的,于是通过自定义WindowManager+协程方式实现了此需求.

在这里插入图片描述

1.showToast方法如下:

object ToastUtils {
    private var toastJob :Job ?= null

   fun Context.showToast(message: String, duration: Int = Toast.LENGTH_SHORT, delayTime: Long = 2000L) {
        val toast = Toast.makeText(this@showToast, message, duration)
        toast.show()

   
        toastJob?.cancel()
        toastJob = CoroutineScope(Dispatchers.Main).launch {
            delay(delayTime)
            toast.cancel()
        }
    }
}

2.使用示例:

    private fun initViews() {
        val textView = findViewById<TextView>(R.id.tv_test)
        textView.setOnClickListener {
            mCountdownJob = countDownCoroutines(10, lifecycleScope,
                onTick = { second ->
                    textView.text = buildString {
                        append(second)
                        append("s后重发")
                    }
                }, onStart = {
                    // 倒计时开始
                }, onFinish = {
                    // 倒计时结束,重置状态
                    textView.text = buildString {
                        append("发送验证码")
                    }
                })

            showToast("祝大家国庆节快乐,万事如意",1,1000L * 10)
        }
    }

3.实现的效果如下:

可以看到虽然显示了Toast,但是5s就消失了,设置显示时间和动态传入10s都是不行的。
在这里插入图片描述

4.自定义Toast弹框(协程):

使用协程实现

package com.cloud.customtoastdemo.toast

import android.content.Context
import android.graphics.PixelFormat
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.cloud.customtoastdemo.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * @auth: njb
 * @date: 2024/10/13 16:56
 * @desc: 描述
 */
object EasyToast {
    private var easyToastView: View? = null
    private var windowManager: WindowManager? = null
    private var mToastJob:Job ?= null
    private val TAG = "EasyToast"
    /**
     *
     * @param context 上下文
     * @param message 提示内容消息
     * @param duration 可动态设置在的显示时间
     * @param gravity 显示位置 top、center、bottom
     */
    fun showCustomToast(context: Context, message: String, duration: Int, gravity: Int) {
        windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

        if (easyToastView == null) {
            easyToastView = inflater.inflate(R.layout.custom_easy_toast, null)
            val textView = easyToastView?.findViewById<TextView>(R.id.tv_message)
            textView?.text = message
        }

        val params = WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_APPLICATION,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
        )

        params.gravity = gravity
        params.x = 0
        params.y = 0

        if (easyToastView?.parent == null) {
            windowManager?.addView(easyToastView, params)
        }
        mToastJob?.cancel()
        mToastJob = CoroutineScope(Dispatchers.Main).launch {
            delay(duration.toLong())
            Log.d(TAG, "时间到了结束弹框$duration")
            if (easyToastView != null) {
                windowManager?.removeView(easyToastView)
                easyToastView = null
            }
        }
    }

    fun cancelEasyToast() {
        if (easyToastView != null) {
            windowManager?.removeView(easyToastView)
            easyToastView = null
        }
        mToastJob?.cancel()
    }
}

5.自定义Toast弹框(Handler):

使用Handler实现:

package com.cloud.customtoastdemo.toast

import android.content.Context
import android.graphics.PixelFormat
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.cloud.customtoastdemo.R

/**

 * @auth: njb

 * @date: 2024/10/13 16:56

 * @desc: 描述
   */
   object EasyToast {
   private var toastView: View? = null
   private var easyToastView: View? = null
   private var windowManager: WindowManager? = null
   private val handler = Handler(Looper.getMainLooper())
   private lateinit var runnable: Runnable

   /**
    *

    * @param context 上下文

    * @param message 提示内容

    * @param message 提示内容消息

    * @param duration 显示时间

    * @param gravity 显示位置

    * @param gravity 显示位置 top、center、bottom
      */
      fun showToast(context: Context, message: String, duration: Int, gravity: Int) {
      windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
      val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

      if (toastView == null) {
          toastView = inflater.inflate(R.layout.custom_toast, null)
          val textView = toastView?.findViewById<TextView>(R.id.tv_message)
      if (easyToastView == null) {
          easyToastView = inflater.inflate(R.layout.custom_toast, null)
          val textView = easyToastView?.findViewById<TextView>(R.id.tv_message)
          textView?.text = message
      }

      val params = WindowManager.LayoutParams(
          WindowManager.LayoutParams.WRAP_CONTENT,
          WindowManager.LayoutParams.WRAP_CONTENT,
          WindowManager.LayoutParams.TYPE_APPLICATION,
          WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
          PixelFormat.TRANSLUCENT
      )

      params.gravity = gravity
      params.x = 0
      params.y = 0

      if (toastView?.parent == null) {
          windowManager?.addView(toastView, params)
      if (easyToastView?.parent == null) {
          windowManager?.addView(easyToastView, params)
      }

      runnable = Runnable {
          if (toastView != null) {
              windowManager?.removeView(toastView)
              toastView = null
          if (easyToastView != null) {
              windowManager?.removeView(easyToastView)
              easyToastView = null
          }
          handler.removeCallbacks(runnable)
      }
      handler.postDelayed(runnable!!, duration.toLong())
      }

   fun cancelEasyToast() {
       runnable?.let {
           handler.removeCallbacks(it)
       }
       if (toastView != null) {
           windowManager?.removeView(toastView)
           toastView = null
       if (easyToastView != null) {
           windowManager?.removeView(easyToastView)
           easyToastView = null
       }
   }
   }

6.使用示例:

package com.cloud.customtoastdemo

import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.cloud.customtoastdemo.contants.Constants
import com.cloud.customtoastdemo.toast.EasyToast
import com.cloud.customtoastdemo.toast.ToastUtils.showToast
import com.cloud.customtoastdemo.utils.CountDownUtils.countDownCoroutines
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

private var mCountdownJob: Job? = null
private val TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    enableEdgeToEdge()
    setContentView(R.layout.activity_main)
    ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
        val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
        v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
        insets
    }
    initViews()
}

private fun initViews() {
    val textView = findViewById<TextView>(R.id.tv_test)
    textView.setOnClickListener {
        mCountdownJob = countDownCoroutines(10, lifecycleScope,

            onTick = { second ->
                Log.d(TAG, "toast显示时间$second")

                textView.text = buildString {
                    append(second)
                    append("s后重发")
                }
            }, onStart = {
                // 倒计时开始
            }, onFinish = {
                // 倒计时结束,重置状态
                textView.text = buildString {
                    append("发送验证码")
                }
            })

/*
lifecycleScope.launch {
delay(1000)
showToast(“祝大家国庆节快乐,万事如意”,1,1000L * 10)
}
*/

        EasyToast.showCustomToast(
            this@MainActivity,
            message = buildString {
                append("祝大家国庆节快乐,万事如意")
            },
            duration = Constants.TOAST_SHOW_TIME,
            gravity = Gravity.TOP
        )
    }
}

}

7.实现效果如下:

在这里插入图片描述

在这里插入图片描述

8.日志打印:

在这里插入图片描述

在这里插入图片描述

9.总结:

从上面的截图可以看出基本上是满足要求的,显示了10sToast提示才消失,至于这个显示时间你可以根据自己的需求动态设置,我这里也没有设置默认时长,尝试过利用反射修改Toast的显示时间和协程delpay方式设置显示时间都没有生效,所以采用WindowManager+协程的方式,当然使用Handler+dialog也可以,今天的内容就到这里,如何实现动态显示Toast时长,打卡收工,关机睡觉.

10.demo地址如下:

https://gitee.com/jackning_admin/custom-toast-demo

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2221342.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

尚硅谷spark学习

p4 快速上手 -开发环境准备

基于工业互联网平台的智能工厂辅助制造企业数字化转型

制造业数字化转型已是大势所趋&#xff0c;工业互联网平台对于制造业数字化转型的支撑作用将会越来越强&#xff0c;其应用为制造企业生产和运营优化的能力提升提供了探索应用模式和路径。平台的不断创新和应用突破&#xff0c;将不断为制造业的升级转型赋能。实施制造业数字化…

C#线程详解及应用示例

简介 在编写应用程序实现业务功能过程中&#xff0c;为解决吞吐量和响应效率的问题&#xff0c;我们会用到多线程、异步编程两项重要的技术。通过它们来提高应用程序响应和高效。应用程序每次运行都会启动一个进程&#xff08;进程是一种正在执行的程序&#xff09;&#xff0…

基于node.js宜家宜业物业管理系统【附源码】

基于node.js宜家宜业物业管理系统 效果如下&#xff1a; 系统首页界面 业主登录界面 停车位页面 小区公告页面 管理员登录界面 管理员功能界面 物业管理员管理界面 缴费信息管理界面 物业管理员功能界面 研究背景 近年来互联网技术飞速发展&#xff0c;给人们的生活带来了极…

【数据分享】全国金融业-股票发行量和筹资额(1991-2021年)

数据介绍 一级标题指标名称单位金融业股票发行量亿股金融业A股发行量亿股金融业H股,N股发行量亿股金融业B股发行量亿股金融业股票筹资额亿元金融业A股筹资额亿元金融业配股筹资额亿元金融业H股,N股筹资额亿元金融业B股筹资额亿元 注&#xff1a;本文中的数据仅为示例&#xf…

Burp Suite Professional 2024.9 for macOS x64 ARM64 - 领先的 Web 渗透测试软件

Burp Suite Professional 2024.9 for macOS x64 & ARM64 - 领先的 Web 渗透测试软件 世界排名第一的 Web 渗透测试工具包 请访问原文链接&#xff1a;https://sysin.org/blog/burp-suite-pro-mac/ 查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&#xff1…

【数据结构】分治算法经典: 快速排序详解

快速排序&#xff08;Quicksort&#xff09;是一种高效的排序算法&#xff0c;最早由Tony Hoare在1960年提出。它采用了分治&#xff08;Divide and Conquer&#xff09;策略&#xff0c;平均时间复杂度为 O ( n log ⁡ n ) O(n \log n) O(nlogn)&#xff0c;在大多数实际应用…

双十一开启极速达夜派;黑神话获泰国年度最佳游戏;AI 模型可帮助识别 17000 多种疾病的候选药物....| 网易数智日报

双 11 菜鸟在北京、上海、广州、杭州等城市开启「预售极速达夜派」服务 10 月 21 日&#xff0c;菜鸟在北京、上海、广州、杭州等城市开启「预售极速达夜派」服务&#xff0c;批量大促包裹实现小时级送达。 据介绍&#xff0c;在消费者支付尾款前&#xff0c;菜鸟供应链就已经…

项目结构(后端+前端)(若依)

项目结构&#xff08;后端前端&#xff09; 文章目录 项目结构&#xff08;后端前端&#xff09;前言一、后端结构1.若依 二、前端结构1. 总结 前言 方便了解项目结构 提示&#xff1a;以下是本篇文章正文内容&#xff1a; 一、后端结构 1.若依 com.ruoyi ├── ruoyi-adm…

【C++干货篇】——类和对象的魅力(四)

【C干货篇】——类和对象的魅力&#xff08;四&#xff09; 1.取地址运算符的重载 1.1const 成员函数 将const修饰的成员函数称之为const成员函数&#xff0c;const修饰成员函数放到成员函数参数列表的后面。const实际修饰该成员函数隐含的this指针&#xff08;this指向的对…

Flutter Container容器组件实战案例

The Container widget is your design toolkit. It’s like the master builder that helps you structure and style your UI elements with precision. Whether you’re creating simple designs or complex layouts, the Container is your trusty tool for the job. “容器…

全能大模型GPT-4o体验和接入教程

GPT-4o体验和接入教程 前言一、原生API二、Python LangchainSpring AI总结 前言 Open AI发布了产品GPT-4o&#xff0c;o表示"omni"&#xff0c;全能的意思。 GPT-4o可以实时对音频、视觉和文本进行推理&#xff0c;响应时间平均为 320 毫秒&#xff0c;和人类之间对…

【C++篇】深度解析类与对象(上)

目录 引言 一、类的定义 1.1类定义的基本格式 1.2 成员命名规范 1.3 class与struct的区别 1.4 访问限定符 1.5 类的作用域 二、实例化 2.1 类的实例化 2.2 对象的大小与内存对齐 三、this 指针 3.1 this指针的基本用法 3.2 为什么需要this指针&#xff1f; 3.3 t…

Java毕业设计 基于SpringBoot发卡平台

Java毕业设计 基于SpringBoot发卡平台 这篇博文将介绍一个基于SpringBoot发卡平台&#xff0c;适合用于Java毕业设计。 功能介绍 首页 图片轮播 商品介绍 商品详情 提交订单 文章教程 文章详情 查询订单  查看订单卡密 客服   后台管理 登录 个人信息 修改密码 管…

成都爱尔胡建斌院长讲解年纪大眼花?小心黄斑变性!

中老年朋友觉得年龄增加后&#xff0c;眼睛出现模糊是常态&#xff0c;但是眼花不止“老花眼”一种&#xff0c;要小心的是眼底病变&#xff01; 眼花的形式有很多种&#xff0c;如果视线中间出现暗点视物变得模糊&#xff0c;很难看清周围的人脸&#xff0c;在看书看手机这种…

MATLAB(Octave)混电动力能耗评估

&#x1f3af;要点 处理电动和混动汽车能耗的后向和前向算法模型(simulink)&#xff0c;以及图形函数、后处理函数等实现。构建储能元数据信息&#xff1a;电池标称特性、电池标识符等以及静止、恒定电流和恒定电压等特征阶段。使用电流脉冲或要识别的等效电路模型类型配置阻抗…

jmeter学习(6)逻辑控制器-循环

循环执行 1、循环读取csv文件的值 2、foreach 读取变量&#xff0c;变量数字后缀有序递增&#xff0c;通过counter实现 ${__V(typeId${typeIdNum})} beansell断言 String typeIdNum vars.get("typeIdNum"); String response prev.getResponseDataAsString(); …

MAC 安装HomeBrew-亲自尝试,100%会成功

文章来自这里: https://zhuanlan.zhihu.com/p/620975942 安装指令&#xff1a; /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"执行完成后&#xff0c;输入下列命令验证 brew --version

AcWing 875:快速幂

【题目来源】https://www.acwing.com/problem/content/877/【题目描述】 给定 组 &#xff0c;对于每组数据&#xff0c;求出 的值。【输入格式】 第一行包含整数 。 接下来 行&#xff0c;每行包含三个整数 。【输出格式】 对于每组数据&#xff0c;输出一个结果&#xff0…

初阶数据结构【3】--单链表(比顺序表还好的一种数据结构!!!)

本章概述 前情回顾单链表实现单链表彩蛋时刻&#xff01;&#xff01;&#xff01; 前情回顾 咱们在上一章博客点击&#xff1a;《顺序表》的末尾&#xff0c;提出了一个问题&#xff0c;讲出了顺序表的缺点——有点浪费空间。所以&#xff0c;为了解决这个问题&#xff0c;我…