Android实现自定义Toast,并且防止重复显示
来源:互联网 发布:银行家算法 0 4 2 0 编辑:程序博客网 时间:2024/05/29 08:24
此项目地址:https://github.com/chenglin198751/BaseMyProject
这个项目里有个文件就是源码:https://github.com/chenglin198751/BaseMyProject/blob/master/app/src/main/java/widget/MyToast.java
我们在用系统的Toast的时候,总是会发现连续多次点击按钮,就会一直不停的弹,完全停不下来的节奏。那能不能无论点击多少次,就只弹一次呢?
而且我们还想自己定义Toast样式,系统默认样式太难看了。
我就看了下Toast的源码,源码很简单,大概看了十来分钟,就知道了如何自定义。下面贴的是自定义的代码,代码很简单:
/** * Created by chenglin on 2017-7-24. */public class MyToast { private static Toast mToast; public static void show(String text) { TextView msgTv = null; if (mToast == null) { mToast = new Toast(MyApplication.getApp()); View view = View.inflate(MyApplication.getApp(), R.layout.my_toast_layout, null); msgTv = (TextView) view.findViewById(R.id.message); msgTv.setText(text); mToast.setView(view); mToast.setDuration(Toast.LENGTH_SHORT); } else { msgTv = (TextView) mToast.getView().findViewById(R.id.message); msgTv.setText(text); } mToast.show(); }}其中的布局文件你可以自己随意的写,我上面用到的布局文件代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/toast_bg" android:orientation="vertical"> <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginTop="8dp" android:gravity="center" android:textColor="@color/text_color_white" android:textSize="15dp" tools:text="我是自定义Toast" /></LinearLayout>
其中的@drawable/toast_bg 的代码如下:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="4dp" /> <solid android:color="#82000000" /></shape>
如果你觉得帮到了你,请给作者打赏一口饭吃:
阅读全文
1 0
- Android实现自定义Toast,并且防止重复显示
- 如何防止android Toast重复显示?
- 如何防止android Toast重复显示
- Android 防止多次点击,Toast重复显示
- Android Toast防止重复显示的方法
- android,防止toast重复显示的方法
- 安卓Toast自定义及防止重复显示
- 安卓Toast自定义及防止重复显示
- Android中防止Toast重复显示的问题
- Android 特殊的单例Toast(防止重复显示)
- 防止android Toast重复提示
- 显示全局的Toast 防止toast重复弹出
- 自定义toast、toast重复显示、切换界面toast自动消失
- 自定义toast、toast重复显示、切换界面toast自动消失
- 自定义Toast,防止层叠显示问题,和自定义Toast样式
- Android Toast 重复显示问题
- Android toast重复显示问题
- Android 自定义Toast实现
- 静态缓存和动态缓存的比较
- Hexo+GitHubPage搭建博客
- hts engine语音识别
- Linux下MD5移植和编程
- 1.2数据结构的历史和在计算机学科中的地位
- Android实现自定义Toast,并且防止重复显示
- 端口分类
- Oracle数据库与MySQL数据库的区别
- Angular
- Mysql监控工具–mytop
- /python课程设计/读取文件遇到的问题
- 深入理解朴素贝叶斯(Naive Bayes)
- Java事务三种类型:JDBC事务、JTA(Java Transaction API)事务、容器事务
- 剑指offer(1)--最小的k个数