GithubHelp home page GithubHelp logo

ximenchuipaopao / toastutils Goto Github PK

View Code? Open in Web Editor NEW

This project forked from getactivity/toaster

0.0 0.0 0.0 45.5 MB

Android 吐司框架,专治 Toast 各种疑难杂症

License: Apache License 2.0

Java 100.00%

toastutils's Introduction

吐司框架

集成步骤

  • 在项目根目录下的 build.gradle 文件中加入
buildscript {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
  • 在项目 app 模块下的 build.gradle 文件中加入
android {
    // 支持 JDK 1.8
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    // 吐司框架:https://github.com/getActivity/ToastUtils
    implementation 'com.github.getActivity:ToastUtils:9.2'
}

初始化框架

ToastUtils.init(this);

显示 Toast

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

取消 Toast

ToastUtils.cancel();

其他 API

// 设置 Toast 布局及样式
ToastUtils.setStyle(IToastStyle<?> style);

// 设置 Toast 重心和偏移
ToastUtils.setGravity(int gravity, int xOffset, int yOffset);

// 设置 Toast 拦截器
ToastUtils.setInterceptor(IToastInterceptor interceptor);

// 设置 Toast 策略
ToastUtils.setStrategy(IToastStrategy strategy);

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

框架亮点

  • 一马当先:首款适配 Android 11 的吐司框架,使用者无需关心适配过程

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

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

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

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

  • 性能最佳:使用懒加载模式,只在显示时创建 Toast,不占用 Application 启动时间

  • 体验最佳:显示下一个 Toast 会取消上一个 Toast 的显示,真正做到即显即示

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

关于通知栏权限

  • 本框架已经完美解决这个问题,即使没有通知栏权限的情况下也能在前台显示 Toast

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

如何替换项目中已有的原生 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

new Toast

温馨提示:框架意在解决一些常规的 Toast 需求,如果是有一些特殊的定制化需求请配搭 XToast 悬浮窗框架使用

作者的其他开源项目

微信公众号:Android轮子哥

Android 技术分享 QQ 群: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.