当前位置: 首页 > news >正文

重庆网站搭建无忧网

重庆网站搭建,无忧网,凡科2网站需要备案吗,做网站seo推广公司Android使用协程实现自定义Toast弹框 ​ 最近有个消息提示需要显示10s,刚开始使用协程写了一个shoowToast方法#xff0c;传入消息内容、显示时间和toast显示类型即可#xff0c;以为能满足需求#xff0c;结果测试说只有5s#xff0c;查看日志和源码发现Android系统中Toa…Android使用协程实现自定义Toast弹框 ​ 最近有个消息提示需要显示10s,刚开始使用协程写了一个shoowToast方法传入消息内容、显示时间和toast显示类型即可以为能满足需求结果测试说只有5s查看日志和源码发现Android系统中Toast显示有2种类型Toast.LENGTH_SHORT和Toast.LENGTH_LONG分别代表Toast消息显示的时间为短暂大约2秒和长时间大约3.5秒这和我们所需要的还是有很大差距的于是通过自定义WindowManager协程方式实现了此需求. 1.showToast方法如下 object ToastUtils {private var toastJob :Job ? nullfun Context.showToast(message: String, duration: Int Toast.LENGTH_SHORT, delayTime: Long 2000L) {val toast Toast.makeText(thisshowToast, message, duration)toast.show()toastJob?.cancel()toastJob CoroutineScope(Dispatchers.Main).launch {delay(delayTime)toast.cancel()}} }2.使用示例 private fun initViews() {val textView findViewByIdTextView(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.toastimport 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? nullprivate var windowManager: WindowManager? nullprivate var mToastJob:Job ? nullprivate 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 WindowManagerval inflater context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflaterif (easyToastView null) {easyToastView inflater.inflate(R.layout.custom_easy_toast, null)val textView easyToastView?.findViewByIdTextView(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 gravityparams.x 0params.y 0if (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.toastimport 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? nullprivate var easyToastView: View? nullprivate var windowManager: WindowManager? nullprivate 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 WindowManagerval inflater context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflaterif (toastView null) {toastView inflater.inflate(R.layout.custom_toast, null)val textView toastView?.findViewByIdTextView(R.id.tv_message)if (easyToastView null) {easyToastView inflater.inflate(R.layout.custom_toast, null)val textView easyToastView?.findViewByIdTextView(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 gravityparams.x 0params.y 0if (toastView?.parent null) {windowManager?.addView(toastView, params)if (easyToastView?.parent null) {windowManager?.addView(easyToastView, params)}runnable Runnable {if (toastView ! null) {windowManager?.removeView(toastView)toastView nullif (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 nullif (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 findViewByIdTextView(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(thisMainActivity,message buildString {append(祝大家国庆节快乐万事如意)},duration Constants.TOAST_SHOW_TIME,gravity Gravity.TOP)} }} 7.实现效果如下 8.日志打印 9.总结 从上面的截图可以看出基本上是满足要求的显示了10sToast提示才消失至于这个显示时间你可以根据自己的需求动态设置我这里也没有设置默认时长尝试过利用反射修改Toast的显示时间和协程delpay方式设置显示时间都没有生效所以采用WindowManager协程的方式当然使用Handlerdialog也可以今天的内容就到这里如何实现动态显示Toast时长打卡收工关机睡觉. 10.demo地址如下 https://gitee.com/jackning_admin/custom-toast-demo
http://www.hkea.cn/news/14493575/

相关文章:

  • 建筑设计方案网站做一下网站需要什么时候开始
  • 网站开发如何报价安徽网站制作公司
  • 香河住房与建设局网站山东省建设安全监督站的网站
  • 地方门户类网站有哪些微信小程序开发教程从零开始
  • 广东宏福建设有限公司网站网页升级中紧急自动转跳中
  • 厦门做网站找哪家公司研发管理系统软件
  • 通了网站建设静态网站什么样
  • 西安网站建设工程抖音小程序助手
  • dede做的网站怎样去换模版谁有恶意点击软件
  • 网站建设的实训报告的实训感受动画网站建设
  • 网站服务器租赁费用表格用jsp进行网站开发
  • 学院网站建设项目概述温州专业营销网站
  • 如何构建电子商务网站如何优化培训方式
  • 商务网站建设综合实训wordpress 缩进
  • 小米手机官方网站朝阳网站seo
  • 网站建设与管理专业就业前景蓝色系的网站
  • 深圳市企业网站建设价格宽带专家网站
  • 网站设计工资一般多少东莞seoseo优化排名
  • 网站开发是啥了html旅游网页完整代码
  • 推广网站文案外贸企业网站管理系统
  • r2网站做生存分析莱芜新闻视频回放
  • 自己做网站可以揽业务吗济南专门做网站的公司
  • 昌吉北京网站建设工作室可以做什么行业
  • 河北邢台手机网站建设wordpress网页特效
  • 做网站推广电话网站建设培训视频教程
  • 应不应该购买老域名建设新网站网站设计总结
  • 聊城企业做网站推广公司注册公司代理
  • 域名注册了后怎么建设网站seo搜索引擎优化论文
  • 上海 网站平台开发crm客户管理系统模块
  • 了解网站开发的一般过程建设外国商城网站