この記事ではダイアログに基本的なリスト を表示する方法を紹介します。
>> Kotlin バージョンはこちら
>>さまざまなダイアログの使い方はこちら
開発環境 Android Studio Giraffe | 2022.3.1 Patch 2 Android Emulator Nexus 4 (API 34) minSdk 19 targetSdk 34
プロジェクトについて Sample という名前でプロジェクトを作成しています。
ファイルは
activity_main.xml
MainActivity.java
MyDialogFragment.java
があることを前提に進めていきます。
MyDialogFragment.java の作成方法はダイアログを表示する方法 ① 基本型(Java編) をお読みください。
解説 1. ダイアログを作成する ダイアログは2種類のデザインがあります。
まずは普通の AlertDialog でリストを作成してみましょう。
MyDialogFragment.java を開いて、以下のようにダイアログを作成します。
This file contains 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.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
public class MyDialogFragment extends DialogFragment {
String[] choices = {"りんご", "ばなな", "みかん", "ぶどう", "いちご"};
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
return new AlertDialog.Builder(requireActivity())
.setTitle("好きな果物は?")
.setItems(choices, (dialogInterface, i) ->
Toast.makeText(
requireActivity(),
String.format("「%s」を選択しました。", choices[i]),
Toast.LENGTH_SHORT)
.show())
.create();
}
}
View the code on Gist .
14行目:項目の用意 リストに表示する項目を用意しています。
19行目: デザインを変える MaterialAlertDialog にする場合は、19行目を以下のように変更してください。
return new MaterialAlertDialogBuilder ( requireActivity ( ) )
21~26行目:項目をセット&タップイベント setItems メソッド を使って、14行目に用意した項目 choices をセットします。
基本のリストでは、項目を1つだけ選択する ことができます。
ここではタップした項目を Toast 表示するコードを書いています。
ボタンについて 「OK」「キャンセル」などのボタンを表示することも可能です。
ダイアログを表示する方法 ① 基本型(Java編) をお読みください。
3. ダイアログを開くボタンを用意する activity_main.xml にダイアログを開くボタンを用意します。
This file contains 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.java に8~11行目を追加します。
This file contains 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
public class MainActivity extends AppCompatActivity {
@ Override
protected void onCreate (Bundle savedInstanceState ) {
super .onCreate (savedInstanceState );
setContentView (R .layout .activity_main );
findViewById (R .id .btn ).setOnClickListener (view -> {
DialogFragment dialogFragment = new MyDialogFragment ();
dialogFragment .show (getSupportFragmentManager (), "my_dialog" );
});
}
}
View the code on Gist .
10行目 dialogFragment. show ( getSupportFragmentManager ( ) , "my_dialog" ) ;
DialogFragment クラスの show メソッドでダイアログを表示します。
一つ目の引数には、フラグメントを管理しているオブジェクトを getSupportFragmentManager() で取得してセットしています。
二つ目の引数は、このダイアログフラグメントのタグです。このフラグメントを識別するために使う名前のようなものです。
6. アプリを実行 アプリを実行してみましょう。
ボタンをタップするとダイアログが表示され、リスト項目を選択すると画面下に Toast が表示されます。
その他のダイアログの使い方は以下の記事をお読みください。
ABOUT ME
書籍やオンライン講座でプログラミングを勉強してフリーランスのプログラマーになりました。
このサイトでは「わかりやすく・シンプル」をモットーに、プログラミングの基礎からアプリ開発まで紹介します。
独学でプログラミングを勉強をしている方、基礎は勉強したけれど次に何をすれば良いか分からない...という方のお役に立てるサイトを目指しています。
主な使用言語:Java / Kotlin / PHP
>> 詳しいプロフィール
>> お問い合わせ
>> 書籍を出版しました!
COMMENT
投稿いただいたコメントは管理者の承認後に表示されます。
コードやエラーに関するご質問の場合は、以下の3点
をできるだけ具体的に必ず書いてください。