android中自定义 toast,android 自定义Toast样式和显示方式
问题:
1.android 开发中如果不停的触发显示Toast,会造成Toast一个接一个的弹出,非常影响用户体验。
2.android设备有千万个,每个设备的Toast的背景有可能不一样,造成在应用内显示时造成显示不够清晰
在这里我封装了一个Toast工具类可以实现弹出下一个Toast时检查当前是否有显示Toast,有的话重用当前Toast,避免重复弹出。
可以自定义Toast显示的view,实现你想要的样式,实现领导的梦想!!!
Toast工具类
/**
* Created by Aaron on 2019/07/18.
*
* 弹出toast提示工具类
*/
object ToastUtils {
private var mToast: Toast? = null //toast样式
private val mMsg: String? = null //上一次弹出的内容
private var mToastView: ToastView? = null //自定义view
private var mToastGravity:Int = -1 //位置
/**
* 弹出提示
* @param msg 提示信息
* @param time 显示时间
*/
fun showToast(msg: String?, time: Int, context: Context?) {
if (mToast == null || mMsg != null && msg != mMsg) {
mToast = Toast.makeText(context, msg, time)
if (mToastView != null) {
mToast!!.view = mToastView
mToastView!!.setText(msg!!)
} else {
mToast!!.setText(msg)
}
} else {
if (mToastView != null && mToast!!.view != mToastView) {
mToast!!.view = mToastView
}
if (mToastView != null) {
mToastView!!.setText(msg!!)
} else {
mToast!!.setText(msg)
}
mToast!!.duration = time
}
if (mToastGravity != -1) {
mToast!!.setGravity(mToastGravity, 0, 0)
}
//不设置的话,最高显示到状态栏下面
mToast!!.view.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
mToast!!.show()
}
/**
* 弹出提示信息
* @param msgId 提示信息id
* @param time 显示时间
*/
fun showToast(msgId: Int, time: Int, context: Context?) {
showToast(context?.getString(msgId), time, context)
}
/**
* 弹出短时间提示
* @param msg 提示信息
*/
fun showShortToast(msg: String, context: Context?) {
showToast(msg, Toast.LENGTH_SHORT, context)
}
fun showShortToast(msgId:Int, context: Context?) {
showToast(msgId, Toast.LENGTH_SHORT, context)
}
/**
* 弹出长时间提示
* @param msg 提示信息
*/
fun showLongToast(msg: String, context: Context?) {
showToast(msg, Toast.LENGTH_LONG, context)
}
/**
* 关闭当前Toast
*/
fun cancelCurrentToast() {
if (mToast != null) {
mToast!!.cancel()
}
}
fun reToast(msg: String) {
Toast.makeText(JblBaseApplication.sInstance, msg, Toast.LENGTH_SHORT).show()
}
fun reToast(msgId: Int) {
Toast.makeText(JblBaseApplication.sInstance, msgId, Toast.LENGTH_SHORT).show()
}
fun setToastView(context: Context?) {
mToastView = ToastView(context!!)
}
fun setToastGravity(gravity: Int) {
mToastGravity = gravity
}
/**
* 重置toast 信息
*/
fun resetToast() {
mToastView = null
mToastGravity = -1
mToast = null
}
}
自定义view
/**
* Created by Aaron on 2019-07-18.
*
* 自定义toast view
*/
class ToastView : FrameLayout {
private var toastText: TextView? = null
constructor(context: Context) : super(context) {
init(context)
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
init(context)
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init(context)
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(
context,
attrs,
defStyleAttr,
defStyleRes
) {
init(context)
}
private fun init(context: Context) {
addView(View.inflate(context, R.layout.toast_view, null))
toastText = findViewById(R.id.toastText)
}
fun setText(text: String) {
toastText!!.text = text
}
fun setTextSize(size: Int) {
toastText!!.textSize = size.toFloat()
}
fun setTextColor(color: Int) {
toastText!!.setTextColor(color)
}
}
自定义view layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#88000000">
android:id="@+id/toastText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/text_color_white"
android:textSize="30sp"
android:paddingLeft="60dp"
android:paddingTop="10dp"
android:paddingRight="60dp"
android:paddingBottom="10dp"
tools:ignore="MissingConstraints"/>
android中自定义 toast,android 自定义Toast样式和显示方式相关推荐
- Android中导航栏之自定义导航布局
Toolbar系列文章导航 Android中导航栏之Toolbar的使用 Android中导航栏之溢出菜单OverflowMenu Android中导航栏之搜索框SearchView Android中 ...
- Android中如何自己定义吐司(Toast)
Android系统里面有个东西很好用,也很常用,那就是Toast,但是长期使用也会发现,Toast有他的不足之处:形式单一,只有文字,风格不变等等,那么要如何自定义一个Toast呢,我们可以先从分析A ...
- Android中SimpleAdapter的使用—自定义列表
本人初学Android,今天研究到Adapter这块感觉挺有意思的,写了个自定义列表进行测试 首先我们新建一个layout列表布局文件,具体布局可以自己设定. 下面贴上我的自定义布局文件代码 1 &l ...
- Android中实现Bitmap在自定义View中的放大与拖动
一基本实现思路: 基于View类实现自定义View –MyImageView类.在使用View的Activity类中完成OnTouchListener接口,实现对MotionEvent事件的监听与处理 ...
- android 如何去掉自定义标签页,Android中为TextView增加自定义的HTML标签
Android中的TextView,本身就支持部分的Html格式标签.这其中包括常用的字体大小颜色设置,文本链接等.使用起来也比较方便,只需要使用Html类转换一下即可.比如: textView.se ...
- Android中为TextView增加自定义的HTML标签
为什么80%的码农都做不了架构师?>>> Android中的TextView,本身就支持部分的Html格式标签.这其中包括常用的字体大小颜色设置,文本链接等.使用起来也比较方便 ...
- android 中dialog对话框,Android中的对话框dialog
普通对话框 单选对话框 多选对话框 进度条对话框 底部弹出框 1.普通对话框 this 代表当前类 最终继承Context 相当于是子类 getApplicationContext:直接返回的是Con ...
- android中底部弹窗,Android实现从底部弹出的Dialog示例(一)
一.概述 先给大家看一下效果图: 点击中间的显示弹框按钮,从底部弹出来一个对话框,用户可以点击拍照或者从相册选择进行相应的操作,下面看看怎么实现. 二.代码实现 主页面布局文件,很简单,一个按钮,响应 ...
- android中oncreate方法,android开发之onCreate( )方法详解
这里我们只关注一句话:This is where you should do all of your normal static set up.其中我们只关注normal static, normal ...
- Android中list常用方法,Android中的常用控件及其基本用法
TextView的使用方法 布局文件中的配置: 在Activity类中的使用: TextView textView1 = (TextView)findViewById(R.id.textView1); ...
最新文章
- unity发布安卓黑屏_Unity将携十余爆款新游和多个独立游戏亮相ChinaJoy 2020
- 视频直播网站开发千万不能忘的一个知识点
- 为什么在反向传播中感知器初始值不能为0_人工智能可以为我们做什么?世界皆可二分类...
- android模糊后面视频,在安卓手机上怎么制作中间是横视频上下是模糊效果的竖视频?手机视频短片制作...
- Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 't
- 主站系统服务器选择,配电网自动化主站系统的结构_功能及操作系统的选择.doc...
- 康奈尔机器人的肌肉是爆米花做的,果然很有爆发力 | ICRA 2018
- Deep Learning 学习笔记
- 如何运行python 复制粘贴_Python执行选择性粘贴
- 阿尔伯塔大学 计算机科学,阿尔伯塔大学
- acm-(dp计数)Educational Codeforces Round 97 (Rated for Div. 2) F. Emotional Fishermen
- python汉字简繁体转换方法
- 【侯捷 C++ 面向对象高级开发】课程笔记以及个人注释(附带课程资源)
- 科大奥锐密立根油滴实验数据_密立根油滴实验数据处理
- 数字全角转半角VBA
- 近红外荧光染料IRDye 650 NHS Ester/NHS活化酯
- 根据年份和月份来计算天数
- svn提交报错,Error running context: 远程主机强迫关闭了一个现有的连接
- Three.js用鼠标控制场景移动的代码
- 做人做事需牢记20条原则
热门文章
- (Deep learning)深度卷积网络实战——第二部分
- 【问答集锦】联邦学习让隐私保护和海量数据学习兼得!
- idea 导入template_如何将静态导入添加到IntelliJ IDEA实时模板
- Facebook如何“养号”干货分享
- Algorithm:字典序最小问题
- Redis基础(八)——集群
- Java虚拟机(二)——垃圾回收与内存分配
- 西瓜书+实战+吴恩达机器学习(九)监督学习之k近邻 K-Nearest Neighbor
- 直观理解:为什么A为 n 阶满秩方阵时,Ax=0 只有零解?
- OpenSatck Ironic实现裸机管理