mirror of
https://github.com/ankidroid/Anki-Android.git
synced 2024-09-20 03:52:15 +02:00
feat: Add checkBoxPrompt to AlertDialog
This differs from the async prompt as it does not remove the 'message' parameter
This commit is contained in:
parent
ee5e6ccad5
commit
aaedfb53da
@ -20,9 +20,12 @@ package com.ichi2.utils
|
||||
|
||||
import android.content.DialogInterface
|
||||
import android.content.DialogInterface.OnClickListener
|
||||
import android.view.View
|
||||
import android.widget.CheckBox
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import com.ichi2.anki.R
|
||||
import com.ichi2.themes.Themes
|
||||
|
||||
/** Wraps [DialogInterface.OnClickListener] as we don't need the `which` parameter */
|
||||
@ -124,3 +127,33 @@ inline fun AlertDialog.Builder.show(block: AlertDialog.Builder.() -> Unit): Aler
|
||||
this.block()
|
||||
this.show()
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a checkbox to the dialog, whilst continuing to display the value of [message]
|
||||
* @param stringRes The string resource to display for the checkbox label.
|
||||
* @param text The literal string to display for the checkbox label.
|
||||
* @param isCheckedDefault Whether or not the checkbox is initially checked.
|
||||
* @param onToggle A listener invoked when the checkbox is checked or unchecked.
|
||||
*/
|
||||
fun AlertDialog.Builder.checkBoxPrompt(
|
||||
@StringRes stringRes: Int? = null,
|
||||
text: CharSequence? = null,
|
||||
isCheckedDefault: Boolean = false,
|
||||
onToggle: (checked: Boolean) -> Unit
|
||||
): AlertDialog.Builder {
|
||||
if (stringRes == null && text == null) {
|
||||
throw IllegalArgumentException("either `stringRes` or `text` must be set")
|
||||
}
|
||||
val checkBoxView = View.inflate(this.context, R.layout.alert_dialog_checkbox, null)
|
||||
val checkBox = checkBoxView.findViewById<CheckBox>(R.id.checkbox)
|
||||
|
||||
val checkBoxLabel = if (stringRes != null) context.getString(stringRes) else text
|
||||
checkBox.text = checkBoxLabel
|
||||
checkBox.isChecked = isCheckedDefault
|
||||
|
||||
checkBox.setOnCheckedChangeListener { _, isChecked ->
|
||||
onToggle(isChecked)
|
||||
}
|
||||
|
||||
return this.setView(checkBoxView)
|
||||
}
|
||||
|
31
AnkiDroid/src/main/res/layout/alert_dialog_checkbox.xml
Normal file
31
AnkiDroid/src/main/res/layout/alert_dialog_checkbox.xml
Normal file
@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="utf-8"?><!--
|
||||
~ Copyright (c) 2023 David Allison <davidallisongithub@gmail.com>
|
||||
~
|
||||
~ This program is free software; you can redistribute it and/or modify it under
|
||||
~ the terms of the GNU General Public License as published by the Free Software
|
||||
~ Foundation; either version 3 of the License, or (at your option) any later
|
||||
~ version.
|
||||
~
|
||||
~ This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
~ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
~ PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
~
|
||||
~ You should have received a copy of the GNU General Public License along with
|
||||
~ this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
-->
|
||||
|
||||
<!-- source: https://stackoverflow.com/a/9763836/ -->
|
||||
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" >
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/checkbox"
|
||||
style="?android:checkboxStyle"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginHorizontal="18dp" />
|
||||
|
||||
</FrameLayout>
|
Loading…
Reference in New Issue
Block a user