-
-
Save codeforfun-jp/b6bcf491da3f4fb991195b4634250e31 to your computer and use it in GitHub Desktop.
How to create dialog series list checkbox - 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 = {"りんご", "ばなな", "みかん", "ぶどう", "いちご"}; | |
boolean[] choicesChecked = {false, true, false, true, false}; | |
@NonNull | |
@Override | |
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { | |
return new AlertDialog.Builder(requireActivity()) | |
.setTitle("好きな果物は?") | |
.setMultiChoiceItems(choices, choicesChecked, (dialog, i, isChecked) -> { | |
choicesChecked[i] = isChecked; | |
Toast.makeText( | |
requireActivity(), | |
String.format("「%s」を変更しました。", choices[i]), | |
Toast.LENGTH_SHORT) | |
.show(); | |
}) | |
.create(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment