简体   繁体   English

在 kotlin 的 android 中创建对话框的好方法是什么?

[英]What is a good way of creating dialog in android in kotlin?

I have a SettingsActivity.kt as follows:我有一个 SettingsActivity.kt 如下:

class SettingsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setLayout()
        setListeners()

    }

    private fun setLayout() {/* fun to set layout* /}
    private fun setListeners() {
        val day = findViewById<LinearLayout>(R.id.settings_day)
        day.setOnClickListener { myDialog() }

        /* some other dialogs created in similar way */
    }

    private fun myDialog() {
        val prefs = getSharedPreferences("SETTINGS", Context.MODE_PRIVATE)
        var selectedDay = prefs.getInt("day", 1)

        val myBuilder = AlertDialog.Builder(this)
        myBuilder
            .setTitle(R.string.settings_day)
            .setSingleChoiceItems(R.array.days, selectedDay) { _, which ->
                selectedDay = which
            }
            .setPositiveButton(R.string.dialog_ok) { _, _ ->
                val editor = prefs.edit()
                editor
                    .putInt("day", selectedDay)
                    .apply()
            }
            .setNegativeButton(R.string.dialog_cancel) { _, _ -> /* do nothing */ } 

        val theDialog = myBuilder.create()
        theDialog.show()
    }
}

When the orientation of device changes, the dialog disappears.当设备的方向改变时,对话框消失。

I think I have to use DialogFragment, but I have some problems, the official guide at https://developer.android.com/guide/topics/ui/dialogs#kotlin doesn't explain much.我想我必须使用DialogFragment,但是我有一些问题, https://developer.android.com/guide/topics/ui/dialogs#kotlin上的官方指南并没有解释太多。 I am confused where to place the code.我很困惑在哪里放置代码。

Most of the tutorials on DialogFragment are either for custom layout or in java.大多数关于 DialogFragment 的教程要么是针对自定义布局,要么是在 Java 中。

So, can somebody tell me how to convert my code so as to use DialogFragment.那么,有人可以告诉我如何转换我的代码以使用 DialogFragment。 I am having difficulty to understand it from examples.我很难从例子中理解它。

EDIT: Anko is now deprecated, please read this .编辑:Anko 现在已弃用,请阅读此内容

If you're working with Kotlin, the Anko Library may interest you.如果您正在使用 Kotlin, Anko 库可能会让您感兴趣。 It provides an easy way to create Alerts, Dialogs, and other common tasks.它提供了一种创建警报、对话框和其他常见任务的简单方法。

Anko is a Kotlin library which makes Android application development faster and easier. Anko 是一个 Kotlin 库,它使 Android 应用程序开发更快更容易。 It makes your code clean and easy to read, and lets you forget about rough edges of the Android SDK for Java.它使您的代码干净且易于阅读,并让您忘记 Android SDK for Java 的粗糙之处。

1- Adding Anko to your project 1- 将 Anko 添加到您的项目中

To add Anko to you Android Kotlin project, add the dependency to your gradle file要将 Anko 添加到您的 Android Kotlin 项目,请将依赖项添加到您的 gradle 文件

dependencies {
    implementation "org.jetbrains.anko:anko:$anko_version"
}

(If you only want to use it for creating Dialogs, just add anko-commons) : (如果您只想使用它来创建对话框,只需添加 anko-commons):

implementation "org.jetbrains.anko:anko-commons:$anko_version"

Make sure that you have the $anko_version variable in your gradle file at the project level:确保在项目级别的 gradle 文件中有 $anko_version 变量:

ext.anko_version='0.10.8'

2- Using Anko to create an Alert/Dialog 2- 使用 Anko 创建警报/对话框

A simple example:一个简单的例子:

alert { title = "Your Title goes here!"

        customView {
            val nameInput = editText() {hint = "Name?"}

            positiveButton("OK!") {
                if( checkUser(nameInput.text) ) {
                  sayHelloTo(nameInput.text)
                }
            }

            negativeButton("ABORT!") { /* do nothing */ }
        }

  }.show()

Please read Anko Documentation - Dialogs to learn how to use Anko.请阅读Anko 文档 - 对话框以了解如何使用 Anko。

Create a subclass of DialogFragment and override onCreateDialog() to return your AlertDialog , eg just move your myDialog code up to myBuilder.create() there.创建DialogFragment的子类并覆盖onCreateDialog()以返回您的AlertDialog ,例如只需将您的myDialog代码向上移动到myBuilder.create()那里。

When you want do display your DialogFragment , instantiate it and call show on it, passing in a reference to the fragment manager and an (optional) tag.当你想显示你的DialogFragment ,实例化它并调用它的 show ,传递对片段管理器的引用和一个(可选)标签。 For example:例如:

MyDialogFragment().show(requireFragmentManager(), null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM