Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created November 11, 2023 11:41
Show Gist options
  • Save codeforfun-jp/86bc9c6a3faebbe8073bef109bfa1848 to your computer and use it in GitHub Desktop.
Save codeforfun-jp/86bc9c6a3faebbe8073bef109bfa1848 to your computer and use it in GitHub Desktop.
How to create dialog series list checkbox - kotlin
package com.example.sample
import android.app.Dialog
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
class MyDialogFragment : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val choices = arrayOf("りんご", "ばなな", "みかん", "ぶどう", "いちご")
val choicesChecked = booleanArrayOf(false, true, false, true, false)
val dialog = activity?.let {
AlertDialog.Builder(it)
.setTitle("好きな果物は?")
.setMultiChoiceItems(choices, choicesChecked) { dialog, i, isChecked ->
choicesChecked[i] = isChecked
Toast.makeText(
it, "「${choices[i]}」を変更しました。", Toast.LENGTH_SHORT
).show()
}
.create()
}
return dialog ?: throw IllegalStateException("アクティビティがNullです。")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment