kotlin 对话框_使用Kotlin的Android警报对话框

kotlin 对话框

In this tutorial, we’ll be discussing Alert Dialogs and implement them in our Android Application using Kotlin.

在本教程中,我们将讨论警报对话框,并使用Kotlin在我们的Android应用程序中实现它们。

警报对话框 (Alert Dialogs)

Alert Dialog is a window that pops up on the screen. They generally show some information and ask for a user action.

“警报对话框”是一个在屏幕上弹出的窗口。 它们通常会显示一些信息并要求用户采取措施。

There are three core components that build an Alert Dialog.

有三个构建警报对话框的核心组件。

  • Title Text

    标题文字
  • Message Text

    讯息文字
  • Buttons – There are three types of buttons: Positive, Negative, and Neutral

    按钮–共有三种类型的按钮:正,负和中性

To create an AlertDialog we use the AlertDialog.Builder inner class.

要创建AlertDialog,我们使用AlertDialog.Builder内部类。

val alertDialogBuilder = AlertDialog.Builder(this)

We pass the context inside the constructor. Optionally, we can pass another parameter, the alert dialog style.

我们在构造函数内部传递上下文。 (可选)我们可以传递另一个参数,即警告对话框样式。

警报对话框方法 (Alert Dialog Methods)

Some of the methods that can be used on an AlertDialog.

可以在AlertDialog上使用的一些方法。

  • setTitle

    setTitle
  • setMessage

    setMessage
  • setIcon

    setIcon
  • setCustomTitle – Here you can pass a custom view that’ll be put in place of the title part in the alert dialog.

    setCustomTitle –在这里您可以传递自定义视图,该视图将代替警报对话框中的标题部分。
  • setPositiveButton – We pass the string name, as well as Button, clicked callback method here.

    setPositiveButton –我们在此处传递字符串名称以及Button单击的回调方法。
  • setView – used to add a custom view inside the alert dialog.

    setView –用于在警报对话框中添加自定义视图。
  • setList – used to set an array of strings which would be displayed in the form of a List.

    setList –用于设置将以列表形式显示的字符串数组。
  • setMultiChoiceList – again we can set an array but this time we can select multiple items from the List thanks to CheckBox.

    setMultiChoiceList –同样,我们可以设置一个数组,但是这次我们可以通过CheckBox从列表中选择多个项目。
  • setPositiveButtonIcon – set an icon alongside the Button

    setPositiveButtonIcon –在按钮旁边设置图标
  • show() – used to display the AlertDialog

    show()–用于显示AlertDialog
  • setDismissListener – Inside this, you can set the logic to be triggered when the alert dialog is dismissed.

    setDismissListener –在其中,您可以设置关闭警报对话框时要触发的逻辑。
  • setShowListener – set the logic to be triggered when the alert dialog is dismissed.

    setShowListener –设置关闭警报对话框时要触发的逻辑。
  • setCancelable – requires a boolean value. By default all alert dialogs are cancelable on button click or touch outside. If this method is set to false, you need to explicitly cancel the dialog using dialog.cancel() method.

    setCancelable –需要一个布尔值。 默认情况下,单击按钮或触摸外部时,所有警报对话框都是可取消的。 如果此方法设置为false,则需要使用dialog.cancel()方法显式取消对话框。

警报对话框Kotlin代码 (Alert Dialog Kotlin Code)

To use AlertDialog in your Android Studio project, import the following class.

要在Android Studio项目中使用AlertDialog,请导入以下类。

import android.support.v7.app.AlertDialog;

Following Kotlin code is used to create a simple alert dialog.

以下Kotlin代码用于创建简单的警报对话框。

val builder = AlertDialog.Builder(this)
builder.setTitle("Androidly Alert")
builder.setMessage("We have a message")
//builder.setPositiveButton("OK", DialogInterface.OnClickListener(function = x))

builder.setPositiveButton(android.R.string.yes) { dialog, which ->
    Toast.makeText(applicationContext,
            android.R.string.yes, Toast.LENGTH_SHORT).show()
}
        
builder.setNegativeButton(android.R.string.no) { dialog, which ->
    Toast.makeText(applicationContext,
            android.R.string.no, Toast.LENGTH_SHORT).show()
}

builder.setNeutralButton("Maybe") { dial
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值