使用DeliciousToast库在Android中显示更好的Toast

本文概述

  • 1.安装TastyToast
  • 2.展示Toast
  • 完整的例子
Android的默认Toast非常有用, 但是它们非常简单, 并且在使用它们几年后, 你甚至可能讨厌它们。对于每个人来说, 这就是为什么总是有替代Android本机组件的原因, 当你对默认组件感到厌倦时可以使用它们。如果你再次改变主意, 可以随时找回它们!对于Toast通知, 有一个漂亮的替代库, 名为DeliciousToast, 它将帮助你显示新的动画Toast, 从而使你的应用程序与其他应用程序有所不同。
在本文中, 我们将向你介绍如何在Android项目中安装和使用DeliciousToast库。
1.安装TastyToast要在Android项目中安装TastyToast库, 请修改build.gradle文件并添加一个新的依赖关系, 即stastetoast一个:
dependencies {implementation 'com.sdsmdg.tastytoast:tastytoast:0.1.1'}

修改后, 同步项目并重新开始。有关此库的更多信息, 请访问Github上的官方存储库。
2.展示Toast借助DeliciousToast, 你将能够显示6种类型的吐司, 即每种情况:
  1. 成功
  2. 警告
  3. 错误
  4. 信息
  5. default
  6. 混乱
你只需要在需要的类上导入名称空间, 并强制转换DeliciousToast类的静态makeText方法, 提供所需的参数即可:
import com.sdsmdg.tastytoast.TastyToast; // 1. Success messageTastyToast.makeText(getApplicationContext(), "Success message !", TastyToast.LENGTH_LONG, TastyToast.SUCCESS); // 2. Warning messageTastyToast.makeText(getApplicationContext(), "Warning message !", TastyToast.LENGTH_LONG, TastyToast.WARNING); // 3. Error messageTastyToast.makeText(getApplicationContext(), "Error message !", TastyToast.LENGTH_LONG, TastyToast.ERROR); // 4. Info messageTastyToast.makeText(getApplicationContext(), "Info message !", TastyToast.LENGTH_LONG, TastyToast.INFO); // 5. Default messageTastyToast.makeText(getApplicationContext(), "Default message !", TastyToast.LENGTH_LONG, TastyToast.DEFAULT); // 6. Confusion messageTastyToast.makeText(getApplicationContext(), "Confusion message !", TastyToast.LENGTH_LONG, TastyToast.CONFUSING);

完整的例子【使用DeliciousToast库在Android中显示更好的Toast】在activity_main.xml文件中, 我们将具有以下布局, 该布局基本上包含6个按钮, 每个按钮都有一个标识符, 稍后将在代码中使用该标识符来附加onClick事件:
< ?xml version="1.0" encoding="utf-8"?> < android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"> < Buttonandroid:id="@+id/successButton"android:layout_width="wrap_content"android:layout_height="49dp"android:text="Success"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintHorizontal_bias="0.049"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.023" /> < Buttonandroid:id="@+id/warningButton"android:layout_width="wrap_content"android:layout_height="49dp"android:text="Warning"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintHorizontal_bias="0.95"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.023" /> < Buttonandroid:id="@+id/errorButton"android:layout_width="wrap_content"android:layout_height="49dp"android:text="Error"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintHorizontal_bias="0.497"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.023" /> < Buttonandroid:id="@+id/infoButton"android:layout_width="wrap_content"android:layout_height="49dp"android:text="Information"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintHorizontal_bias="0.054"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.143" /> < Buttonandroid:id="@+id/defaultButton"android:layout_width="wrap_content"android:layout_height="49dp"android:text="Default"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintHorizontal_bias="0.551"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.143" /> < Buttonandroid:id="@+id/confusionButton"android:layout_width="wrap_content"android:layout_height="49dp"android:text="Confusion"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintHorizontal_bias="0.95"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.143" /> < /android.support.constraint.ConstraintLayout>

测试吐司的代码是:
package com.yourcompany.yourapp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.sdsmdg.tastytoast.TastyToast; public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 1. Success messageButton buttonSuccess = findViewById(R.id.successButton); buttonSuccess.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {TastyToast.makeText(getApplicationContext(), "Success message !", TastyToast.LENGTH_LONG, TastyToast.SUCCESS); }}); // 2. Warning messageButton buttonWarning = findViewById(R.id.warningButton); buttonWarning.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {TastyToast.makeText(getApplicationContext(), "Warning message !", TastyToast.LENGTH_LONG, TastyToast.WARNING); }}); // 3. Error messageButton buttonDanger = findViewById(R.id.errorButton); buttonDanger.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {TastyToast.makeText(getApplicationContext(), "Error message ...", TastyToast.LENGTH_LONG, TastyToast.ERROR); }}); // 4. Information messageButton buttonInfo = findViewById(R.id.infoButton); buttonInfo.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {TastyToast.makeText(getApplicationContext(), "Info message ...", TastyToast.LENGTH_LONG, TastyToast.INFO); }}); // 5. Default messageButton buttonDefault = findViewById(R.id.defaultButton); buttonDefault.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {TastyToast.makeText(getApplicationContext(), "Default message ...", TastyToast.LENGTH_LONG, TastyToast.DEFAULT); }}); // 6. Confusion messageButton buttonConfusion = findViewById(R.id.confusionButton); buttonConfusion.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {TastyToast.makeText(getApplicationContext(), "Confusion message ...", TastyToast.LENGTH_LONG, TastyToast.CONFUSING); }}); }}

编码愉快!

    推荐阅读