この記事ではダイアログにチェックボックスのリスト を表示する方法を紹介します。
>> Java バージョンはこちら
>>さまざまなダイアログの使い方はこちら
開発環境 Android Studio Giraffe | 2022.3.1 Patch 2 Android Emulator Nexus 4 (API 34) minSdk 19 targetSdk 34
プロジェクトについて Sample という名前でプロジェクトを作成しています。
ファイルは
activity_main.xml
MainActivity.kt
MyDialogFragment.kt
があることを前提に進めていきます。
MyDialogFragment.kt の作成方法はダイアログを表示する方 法 ① 基本型(Kotlin編) をお読みください。
解説 1. ダイアログを作成する ダイアログは2種類のデザインがあります。
まずは普通の AlertDialog でリストを作成してみましょう。
MyDialogFragment.kt を開いて、以下のようにダイアログを作成します。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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です。")
}
}
View the code on Gist .
13行目:項目の用意 リストに表示する項目を用意しています。
14行目:初期のチェック状態 最初からチェックを付けておく項目は true にしておきます。
17行目: デザインを変える MaterialAlertDialog にする場合は、17行目を以下のように変更してください。
MaterialAlertDialogBuilder ( it)
19~24行目:項目をセット&タップイベント setMultiChoiceItems メソッド を使って、13・14行目に用意した項目と初期状態 をセットします。
20行目でチェック状態を更新しておきます。
ここではタップした項目を Toast 表示するコードを書いています。
ボタンについて 「OK」「キャンセル」などのボタンを表示することも可能です。
ダイアログを表示する方法 ① 基本型(Kotlin編) をお読みください。
3. ダイアログを開くボタンを用意する activity_main.xml にダイアログを開くボタンを用意します。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ダイアログを表示"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
View the code on Gist .
このような画面になります。
4. ダイアログを表示する ボタンをタップしたらダイアログを表示するコードを追加します。
MainActivity.kt に6~9行目を追加します。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.btn).setOnClickListener {
val dialogFragment = MyDialogFragment()
dialogFragment.show(supportFragmentManager, "my_dialog")
}
}
}
View the code on Gist .
8行目 dialogFragment. show ( supportFragmentManager, "my_dialog" )
DialogFragment クラスの show メソッドでダイアログを表示します。
一つ目の引数には、フラグメントを管理しているオブジェクトを getSupportFragmentManager() で取得してセットしています。
二つ目の引数は、このダイアログフラグメントのタグです。このフラグメントを識別するために使う名前のようなものです。
6. アプリを実行 アプリを実行してみましょう。
ボタンをタップするとダイアログが表示され、チェックを変更すると画面下に Toast が表示されます。
その他のダイアログの使い方は以下の記事をお読みください。
ABOUT ME
書籍やオンライン講座でプログラミングを勉強してフリーランスのプログラマーになりました。
このサイトでは「わかりやすく・シンプル」をモットーに、プログラミングの基礎からアプリ開発まで紹介します。
独学でプログラミングを勉強をしている方、基礎は勉強したけれど次に何をすれば良いか分からない...という方のお役に立てるサイトを目指しています。
主な使用言語:Java / Kotlin / PHP
>> 詳しいプロフィール
>> お問い合わせ
>> 書籍を出版しました!
COMMENT
投稿いただいたコメントは管理者の承認後に表示されます。
コードやエラーに関するご質問の場合は、以下の3点
をできるだけ具体的に必ず書いてください。