本文概述
- 1.安装TastyToast
- 2.展示Toast
- 完整的例子
在本文中, 我们将向你介绍如何在Android项目中安装和使用DeliciousToast库。
1.安装TastyToast要在Android项目中安装TastyToast库, 请修改build.gradle文件并添加一个新的依赖关系, 即stastetoast一个:
dependencies {implementation 'com.sdsmdg.tastytoast:tastytoast:0.1.1'}
修改后, 同步项目并重新开始。有关此库的更多信息, 请访问Github上的官方存储库。
2.展示Toast借助DeliciousToast, 你将能够显示6种类型的吐司, 即每种情况:
- 成功
- 警告
- 错误
- 信息
- default
- 混乱
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);
}});
}}
编码愉快!
推荐阅读
- Preact(具有相同ES6 API的3KB快速ReactJS替代方案)
- 如何在Android中实现表情符号(SmileyRating)等级栏
- 如何处理Electron Framework中的”拖放文件”功能
- 如何在Electron Framework中使用React Dev Tools
- Matreshka.js(创建单页应用程序的简单JavaScript框架)
- Prepack(用于JavaScript的部分评估程序,可生成有效的JavaScript)
- Carbon(创建和共享代码段精美图片的在线工具)
- Keeweb(与KeePass兼容的免费跨平台密码管理器)
- AppleScript-等待shell脚本完成