android toast_Android Toast

android toast_Android ToastandroidtoastToastmessageisusefultoshownotificationforsmalltimeinandroidapp.Inthistutorial,we’lldiscussandimplementandroidToastmessageexample.Toast消息对在Android应用中显示少量通知非常有用。在本教…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

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">

    <Button
        android:id="@+id/btnBasicToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Basic Toast"/>

    <Button
        android:id="@+id/btnGravityToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Basic Toast With Gravity"/>

    <Button
        android:id="@+id/btnOffsetToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Basic Toast With Gravity And Offset"/>

    <Button
        android: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;

    @Override
    protected 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);
    }

    @Override
    public 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-toasts-output

运行中的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示例项目

Reference: Official Doc.

参考: 官方文件

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

android toast

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/194369.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Linux之ant安装部署「建议收藏」

    Linux之ant安装部署「建议收藏」 今天呢,在这里讲下linux环境下ant的部署,废话不多说,直接进入教程。 首先呢,先安装基础环境Java,在这里就不多说了…..不熟悉的小伙伴可以百度找找,很简单的……. 接下来呢,就开始ant的部署,具体分为如下几个步骤:  1.获取介质:       在apache的官网中直接下载,下载地址为:http://ant.apache.org/    …

    2022年7月24日
    15
  • pycharm 2021.4.14激活码失效_通用破解码「建议收藏」

    pycharm 2021.4.14激活码失效_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    54
  • 浅谈 js 字符串 search 方法

    浅谈 js 字符串 search 方法这是一个很久以前的事情了,好像是安心兄弟在学习js的时候做的练习。具体记不清了,今天就来简单分析下search究竟是什么用的。从字面意思理解,一个是搜索字符串吧。varstr="1

    2022年7月2日
    24
  • 怎样快速在线将pdf文件转换成word

    怎样快速在线将pdf文件转换成word免费pdf转word在线转换器在办公环境下如何将PDF转换成Word,是不少上班族普遍需要了解的问题之一。面对上百份需要处理的文档,其实否不用安装专业的PDF转Word转换器,借助免费PDF转Word在线转换器就能轻松帮你搞定PDF转Word问题。pdf在线转换成wordhttp://app.xunjiepdf.com   pdf word在线转换器最新发布的迅捷免费pdf转word

    2022年6月4日
    34
  • Linux查看进程占用端口号_windows查看进程占用端口

    Linux查看进程占用端口号_windows查看进程占用端口查看linux端口被哪个进程占用的方法:首先查看被占用的端口的进程,并查询进程id;然后根据集成id查询进程,并查看进程详情信息;最后查看进行所在目录,操作进程即可。本教程操作环境:redhatenterpriselinux6.1、DELLG3电脑。查看linux端口被哪个进程占用的方法:1、查询被占用的端口。首先是需要输入命令,查看被占用的端口的进程,netstat-tunpl|g…

    2022年7月27日
    2
  • 基于Qt的音乐播放器(二)切换歌曲,调节音量,调节语速,暂停

    基于Qt的音乐播放器(二)切换歌曲,调节音量,调节语速,暂停切换歌曲,调节音量,调节语速,暂停先说一下,针对上一次的ui界面,这次做了重新设计,第一张是以前的,第二张是现在的设计,不要喷我按钮的ui,都是临时的,后面会用一种风格整体替换,我还加入了皮肤切换,不过还没有实现功能,这个ui也不是最终设计,后期还是会更新的,争取做到最好,说实话,这个设计真是让人头疼,毕竟是把美工的活抢了,哈哈,然后这个ui的设计,我们先不讲,如果需求高的话,会考虑再写一篇有关ui的,完整项目已上传github,自行下载,其他就没有了,我们赶紧进入今天的正题。

    2022年5月24日
    37

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号