简体   繁体   中英

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

I have a SettingsActivity.kt as follows:

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. I am confused where to place the code.

Most of the tutorials on DialogFragment are either for custom layout or in java.

So, can somebody tell me how to convert my code so as to use DialogFragment. I am having difficulty to understand it from examples.

EDIT: Anko is now deprecated, please read this .

If you're working with Kotlin, the Anko Library may interest you. 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. It makes your code clean and easy to read, and lets you forget about rough edges of the Android SDK for Java.

1- Adding Anko to your project

To add Anko to you Android Kotlin project, add the dependency to your gradle file

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

(If you only want to use it for creating Dialogs, just add 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:

ext.anko_version='0.10.8'

2- Using Anko to create an Alert/Dialog

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.

Create a subclass of DialogFragment and override onCreateDialog() to return your AlertDialog , eg just move your myDialog code up to myBuilder.create() there.

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. For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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