使用Kotlin的Android Toast

本文介绍了如何在Android应用中使用Kotlin编程语言创建和定制Toast。内容包括何时使用Toast,创建Toast的基本语法,以及如何从strings.xml设置消息,还包括使用Kotlin扩展功能简化Toast调用的方法和以编程方式创建自定义布局的Toast。
摘要由CSDN通过智能技术生成

Android Toast is a UI widget that pops up on the screen for some time. It’s very similar to a notification, alert, or tooltip message. In this tutorial, we’ll learn how to create a Toast using Kotlin programming language.

Android Toast是一个UI小部件,它会在屏幕上弹出一段时间。 它与通知,警报或工具提示消息非常相似。 在本教程中,我们将学习如何使用Kotlin编程语言创建Toast。

何时在Android应用中使用Toasts? (When to use Toasts in Android app?)

Toast messages are used to show the user something that isn’t critical but might help the user with what’s happening in the application currently. For example, giving a hint for a specific field in the form.

Toast消息用于向用户显示不重要的内容,但可能会帮助用户了解应用程序中当前发生的情况。 例如,为表单中的特定字段提供提示。

创建吐司的语法 (Syntax to Create a Toast)

We can use the Toast class to create a toast message.

我们可以使用Toast类创建一条Toast消息。

Toast.makeText(this, "Androidly Short Toasts", Toast.LENGTH_SHORT).show();

Toast.makeText(this, "Androidly Long Toasts", Toast.LENGTH_LONG).show();

Toast requires three arguments.

吐司需要三个参数。

  1. Context

    语境
  2. Message

    信息
  3. Duration

    持续时间

The show() function is used to display the Toast on the screen.

show()函数用于在屏幕上显示Toast。

We can customize the layout of the toast as well as its duration.

我们可以自定义吐司的布局及其持续时间。

使用Kotlin的Android Toast (Android Toast using Kotlin)

Let’s use the Kotlin to create an android app and create some toast messages.

让我们使用Kotlin创建一个android应用程序并创建一些Toast消息。

First of all, create a new Android Studio Project and include the Kotlin support if you’re using Android Studio 3.0.

首先,如果您使用的是Android Studio 3.0,请创建一个新的Android Studio项目并包括Kotlin支持。

Select an empty activity.

选择一个空的活动。

The custom_toast_layout.xml is used to define a custom layout for the Toast.

custom_toast_layout.xml用于定义Toast的自定义布局。

The la

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值