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

程序员知识网站需要多少钱服务器做网站空间

程序员知识网站需要多少钱,服务器做网站空间,网站建设合作流程图,免费申请网站永久域名Android使用协程实现自定义Toast弹框 ​ 最近有个消息提示需要显示10s,刚开始使用协程写了一个shoowToast方法,传入消息内容、显示时间和toast显示类型即可,以为能满足需求,结果测试说只有5s,查看日志和源码发现Android系统中Toa…

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 ?= nullfun 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.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?.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 = 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?.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 = 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 = 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.yayakq.cn/news/704058/

相关文章:

  • 广州建设银行招聘网站室内设计师在哪里找
  • 网站开发与部署四川建设行业数据共享平台网站问题
  • 酒吧网站模板社交app网站开发
  • 做图片网站会侵权吗代做论文网站好
  • 重新建设网站山西常见网站建设推荐优化
  • 搜索引擎网站的结构连云港企业做网站
  • 宝塔做的网站能不能访问竞价托管公司排名
  • 做网站的如何找业务wordpress小米论坛主题
  • 合肥网站制作公司排名个人求职网站html
  • 企业网站建站做泥网站
  • 有关网站建设的网站wordpress 屏蔽广告
  • 企业网站如何建立室内软装设计软件
  • 长春网站业务哪个公司好公司网站推广方法
  • app网站开发公司的logo网站快速备案
  • ps网站主页按钮怎么做如何保持网站中的图片
  • 网站开发 浏览器景观设计公司名称
  • 广东网站建设多少钱徐州网站建设开发
  • 商务型网站有哪些wordpress媒体库增加分类
  • 个人淘宝客网站网站开发工具简述
  • 天津百度做网站多少钱建站公司服务
  • 买东西最便宜的网站龙海网站建设价格
  • 知名网站用的技术百度竞价推广代运营话术
  • 友点企业网站管理系统忘记密码河源市连平县建设局网站
  • 原创 网站 源码好买卖做网站
  • 网站小游戏怎么做的电子商务是最差的专业吗
  • 上海网站建设设wordpress 二次开发视频教程下载
  • 社交网站开发意义视频互动网站建设
  • iis装网站建设工程八大员考试网站
  • 百度做一个网站多少钱手机网站判断跳转代码怎么写
  • 泰州市高港区建设局网站学校网站做网页飘窗怎么做