Git Product home page Git Product logo

toastutils's Introduction

吐司工具类

码云地址:Gitee

博客地址:只需体验三分钟,你就会跟我一样,爱上这款Toast

已投入公司项目多时,没有任何毛病,可胜任任何需求,点击此处下载Demo

想了解实现原理的可以点击此链接查看:ToastUtils 源码

本框架意在解决一些常见需求,如果是有一些极端的需求推荐使用 XToast

集成步骤

dependencies {
    implementation 'com.hjq:toast:8.8'
}

初始化 Toast

// 在 Application 中初始化
ToastUtils.init(this);

显示 Toast

ToastUtils.show("我是吐司");

其他 API

// 设置Toast布局
ToastUtils.setView();

// 设置吐司重心
ToastUtils.setGravity();

// 获取Toast对象
ToastUtils.getToast();

自定义Toast样式

如果对Toast的默认样式不满意,可以在Application初始化样式,具体可参考ToastBlackStyle类的实现

ToastUtils.initStyle(new IToastStyle());

框架亮点

  • 无需权限:不管有没有授予通知栏权限都不影响吐司的弹出

  • 兼容性强:处理原生 Toast 在 Android 7.1 产生崩溃的历史遗留问题

  • 功能强大:不分主次线程都可以弹出Toast,自动区分资源 id 和 int 类型

  • 使用简单:只需传入文本,会自动根据文本长度决定吐司显示的时长

  • 性能最佳:单例吐司,整个 Toast 只有一个 TextView,并且通过代码创建

  • 体验最优:限制 Toast 短时间内弹出的次数,避免频繁弹出造成不良的用户体验

  • 支持多种样式:默认为黑色样式,夜间模式可使用白色样式,还有仿 QQ 吐司样式

  • 支持自定义样式:吐司(背景、圆角、重心、偏移),文字(大小、颜色、边距)

  • 支持自定义扩展:支持获取 ToastUtils 中的 Toast 对象,支持重新自定义 Toast 布局

  • 支持全局配置样式:可以在 Application 中初始化 Toast 样式,达到一劳永逸的效果

  • 已适配 Android R:Android 11 之后不能弹出自定义样式的 Toast,框架针对这种情况进行了适配

关于通知栏权限

本框架已经完美解决这个问题(禁用通知栏权限后需要重启应用、重启应用、重启应用才能后生效,请以Demo测试为主)

在开启 APP 的同时关闭通知栏权限的情况极少(测试人员才会这么做),所以仅在 Application 初始化的时候才对没有通知栏权限的情况进行判断和兼容

具体解决方案可见:Toast通知栏权限填坑指南

ToastUtils 架构图

如何替换项目中已有的 Toast ?

右击项目,Replace in path,勾选 Regex 选项

Toast\.makeText\([^,]+,\s*(.+{1}),\s*[^,]+\)\.show\(\)

替换使用

ToastUtils.show($1)

包名替换

import android.widget.Toast

import com.hjq.toast.ToastUtils

再全局搜索,手动更换一些没有替换成功的

Toast.makeText

作者的其他开源项目

Android技术讨论Q群:78797078

如果您觉得我的开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 🐵就太👍了。您的支持将鼓励我继续创作:octocat:

License

Copyright 2018 Huang JinQun

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

toastutils's People

Contributors

880634 avatar getactivity avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.