android toast

Toast message is useful to show notification for small time in android app. In this tutorial, we’ll discuss and implement android Toast message example.

Toast消息对在Android应用中显示少量通知非常有用。 在本教程中,我们将讨论并实现android Toast消息示例。

Android Toast (Android Toast)

Android Toast is used to show notification for a particular interval of time at the bottom of the screen by default. Toast message doesn’t block the user interaction and auto disappears after a timeout. The android.widget.Toast class is the subclass of java.lang.Object class.

默认情况下,Android Toast用于在屏幕底部显示特定时间间隔的通知。 Toast消息不会阻止用户交互,并且超时后自动消失。 android.widget.Toast类是java.lang.Object类的子类。

创建基本的Toast消息 (Creating a Basic Toast message)

Android Toast message is created using the method makeText() that is passed with the context, the message, and the duration as shown below:

Android Toast消息是使用方法makeText()创建的,该方法随上下文,消息和持续时间一起传递,如下所示:

Toast toast = Toast.makeText(context, "String goes here", duration);

The context can be of the application or the activity. It’s recommended to use getApplicationContext() to let the Toast be displayed irrespective of the current state of the Activity.

context可以是应用程序或活动的context 。 建议使用getApplicationContext()来显示Toast,而与Activity的当前状态无关。

The duration can be set as Toast.LENGTH_SHORT or Toast.LENGTH_LONG.

duration可以设置为Toast.LENGTH_SHORTToast.LENGTH_LONG

The Toast is displayed using the method show().

使用show()方法show() Toast。

Toast.makeText(getApplicationContext(),"Basic Toast message", Toast.LENGTH_SHORT).show()

定位您的吐司消息 (Positioning your Toast Message)

A standard Toast message appears at the bottom of the screen. We can set our own gravity on a Toast as shown below

屏幕底部显示一条标准的Toast消息。 我们可以在Toast上设置自己的重力,如下所示

toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);

The second and third parameters are used to shift the toast to the right and down respectively by the offset specified.

第二个参数和第三个参数用于将烤面包分别向右和向下移动指定的偏移量。

Toast Android的自定义布局 (Custom Layout for Toast Android)

To create a custom layout we can define the view layout in the XML lets say custom_toast.xml

要创建自定义布局,我们可以在XML中定义视图布局,比如说custom_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"android:id="@+id/custom_toast_container"android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="fill_parent"android:padding="8dp"android:background="#7DA1BC"><ImageView android:src="@android:drawable/stat_notify_error"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="8dp"/><TextView android:id="@+id/text"android:layout_width="wrap_content"android:layout_gravity="center_vertical"android:layout_height="wrap_content"android:textColor="#FFF"/>
</LinearLayout>

In our activity class, we’ll inflate the above layout and set it on the Toast using the method setView().

在我们的活动类中,我们将膨胀以上布局,并使用setView()方法在Toast上进行设置。

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.custom_toast_container));TextView text = layout.findViewById(R.id.text);
text.setText("This is a custom toast");toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

Let’s create an application in which each button would display a different type of Toast message, amongst the ones we’ve just discussed.

让我们创建一个应用程序,其中每个按钮将显示刚刚讨论过的Toast消息的不同类型。

Android Toast消息示例项目结构 (Android Toast Message Example Project Structure)

Android Toast消息代码 (Android Toast Message Code)

The code for the activity_main.xml layout is given below.

下面给出了activity_main.xml布局的代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"xmlns:tools="https://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:layout_gravity="center"android:gravity="center"tools:context="com.journaldev.toasts.MainActivity"><Buttonandroid:id="@+id/btnBasicToast"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Basic Toast"/><Buttonandroid:id="@+id/btnGravityToast"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Basic Toast With Gravity"/><Buttonandroid:id="@+id/btnOffsetToast"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Basic Toast With Gravity And Offset"/><Buttonandroid:id="@+id/btnCustomToast"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Custom Toast"/></LinearLayout>

The code for the MainActivity.java is given below

MainActivity.java的代码如下

package com.journaldev.toasts;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener {Button btnToast, btnGravityToast, btnOffsetToast, btnCustomToast;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnToast = findViewById(R.id.btnBasicToast);btnGravityToast = findViewById(R.id.btnGravityToast);btnOffsetToast = findViewById(R.id.btnOffsetToast);btnCustomToast = findViewById(R.id.btnCustomToast);btnToast.setOnClickListener(this);btnGravityToast.setOnClickListener(this);btnOffsetToast.setOnClickListener(this);btnCustomToast.setOnClickListener(this);}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.btnBasicToast:Toast.makeText(getApplicationContext(), "Basic Toast", Toast.LENGTH_SHORT).show();break;case R.id.btnGravityToast:Toast toast = Toast.makeText(getApplicationContext(), "Toast with Gravity", Toast.LENGTH_SHORT);toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);toast.show();break;case R.id.btnOffsetToast:toast = Toast.makeText(getApplicationContext(), "Toast With Offset", Toast.LENGTH_SHORT);toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 50, 50);toast.show();break;case R.id.btnCustomToast:LayoutInflater inflater = getLayoutInflater();View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.custom_toast_container));TextView text = layout.findViewById(R.id.text);text.setText("This is a custom toast");toast = new Toast(getApplicationContext());toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);toast.setDuration(Toast.LENGTH_LONG);toast.setView(layout);toast.show();break;}}
}

The output of the android toast example application in action is given below.

运行中的android Toast示例应用程序的输出如下。

Did you notice the position change between the Gravity Toast and the one with Offset included?

您是否注意到重力吐司和带有偏移量的吐司之间的位置变化?

This brings an end to toast android tutorial. You can download the final Android Toast Example Project from the link below.

这结束了吐司android教程。 您可以从下面的链接下载最终的Android Toast示例项目

Download Android Toast Example下载Android Toast示例

Reference: Official Doc.

参考: 官方文件 。

翻译自: https://www.journaldev.com/15867/android-toast

android toast

android toast_Android Toast相关推荐

  1. Unity调用安卓Android的Toast

    需求:在游戏中弹窗消息,调起安卓的Toast 项目中需要做Unity和安卓交互时,经常需要通过安卓Toast来做简单的输出,以便于测试. 方法一:Unity中,C#主导 // Unity调用安卓的土司 ...

  2. Android中Toast的用法简介

    2019独角兽企业重金招聘Python工程师标准>>> Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的 ...

  3. android一天一次弹窗,Android自定义Toast,多次弹出时取消上次弹出,最后一次弹出为准...

    下面是编程之家 jb51.cc 通过网络收集整理的代码片段. 编程之家小编现在分享给大家,也给大家做个参考. Android的Toast用队列管理弹出的消息,这个自定义的Toast用于频繁弹出Toas ...

  4. Android的Toast介绍-android学习之旅(三十六)

    Toast简单介绍 Toast是一个很方便的消息提示框.会在桌面显示一个短暂的消息提示.有两个特点: 1.消息不会获得焦点. 2.过一段时间会自己主动消失. Toast的生成步骤 1.调用构造器或者静 ...

  5. android custom toast,Android自定义Toast

    核心代码: package com.huatec.myapplication; import android.content.Context; import android.graphics.Bitm ...

  6. android toast 一直显示,android中toast无法显示问题

    本文记录两种情况下Android的toast无法显示的问题及提供对应的解决方案 关闭通知权限 在Android系统中将通知栏权限,我们可以发现toast无法展示出来(绝大部分机子,oppo和vivo一 ...

  7. android toast设置背景颜色,Android 彩色Toast的实现代码

    Android默认的Toast太丑了,我们来封装一个花里胡哨的Toast吧,就叫ColoredToast. 效果: Toast有一个setView方法,通过它我们可以设置自定义的布局,这里我只是加入了 ...

  8. web 弹出框 类似 android的toast的信息提示

    web js的alert弹出框,有时候并不是我们想要的,但是有时候又想给用户友好的提示,看了android的toast提示框也可在web前端实现,下面是参考一个人的源码稍作修改做的 // JavaSc ...

  9. android toast怎么改变位置,Android 更改 Toast 的默认位置方法

    Android中Toast的默认位置在屏幕靠近底部的位置,这个默认位置有时候并不合适.比如页面上内容较少时,内容一般集中在屏幕上半部分,用户的注意力也集中在屏幕上半部分,默认位置的Toast用户可能没 ...

最新文章

  1. RNN(Recurrent Neural Network)的几个难点
  2. Xap packaging failed , Object reference not set to an instance of an object.
  3. C++Primer::头文件设计基本原则 与 预处理器介绍
  4. css动画执行保持forwards,css3动画如何在动作结束时保持该状态不变
  5. [安卓] 19、一个蓝牙4.0安卓DEMO
  6. 【李宏毅2020 ML/DL】补充:Ensemble: Bagging, Boosting, Adaboost, Gradient Boosting, Stacking
  7. css的checkbox样式,css自定义checkbox样式的方法总结
  8. pgadmin4工具安装及使用
  9. Rhino4.0软件安装教程
  10. MySQL讲义第 45 讲——select 查询之查询练习(三)
  11. python编程课哪个机构最好,python培训比较好的机构
  12. 蜂鸟数据Trochil:论述制定策略的两种主要方法:市场假设和瑞士法郎案例研究-构建更好的策略1
  13. Halcon中关于角度计算和测量拟合的算子详解
  14. 基于Jsp的手机应用商店的设计与实现mysql
  15. u云支付 php05,优云易支付-免签约支付平台-彩虹易支付,1分钟快速接入支付功能...
  16. 怎样有效率地进行外文文献检索?
  17. Web APIs --JavaScript学习笔记(总)(包括DOM、BOM)
  18. 香港理工大学,新设“元宇宙科技”专业
  19. 【面试相关】202006面试总结
  20. Phonetic symbol 双元音 -- əʊ

热门文章

  1. UVA 10391 STL容器的使用
  2. autotools归纳
  3. 工作经验总结:百万数据引发的性能瓶颈问题
  4. 如何避免delete和delete[]的尴尬?
  5. http://code.svnspot.com/ 免费代码托管
  6. Google及其云智慧
  7. [转载] python字典查询功能_Python中的字典功能
  8. [转载] 基本概念:java中的访问修饰符
  9. [转载] 【Java核心技术卷】关于除以0的计算
  10. 字符和字符串在Java中的旅程