diff --git a/app/src/main/java/io/legato/kazusa/ui/widget/number/NumberPickerDialog.kt b/app/src/main/java/io/legato/kazusa/ui/widget/number/NumberPickerDialog.kt index 55d2c49d7..eb08df8fa 100644 --- a/app/src/main/java/io/legato/kazusa/ui/widget/number/NumberPickerDialog.kt +++ b/app/src/main/java/io/legato/kazusa/ui/widget/number/NumberPickerDialog.kt @@ -1,8 +1,15 @@ package io.legato.kazusa.ui.widget.number +import android.app.AlertDialog import android.content.Context +import android.view.View +import android.widget.LinearLayout import android.widget.NumberPicker +import android.widget.TextView +import com.google.android.material.button.MaterialButton import com.google.android.material.dialog.MaterialAlertDialogBuilder +import com.google.android.material.textfield.TextInputEditText +import com.google.android.material.textfield.TextInputLayout import io.legato.kazusa.R import io.legato.kazusa.utils.hideSoftInput @@ -10,9 +17,13 @@ import io.legato.kazusa.utils.hideSoftInput class NumberPickerDialog(context: Context) { private val builder = MaterialAlertDialogBuilder(context) private var numberPicker: NumberPicker? = null + private var editText: TextInputEditText? = null + private var inputLayout: TextInputLayout? = null + private var tvRange: TextView? = null private var maxValue: Int? = null private var minValue: Int? = null private var value: Int? = null + private var useInputMode = false init { builder.setView(R.layout.dialog_number_picker) @@ -40,36 +51,64 @@ class NumberPickerDialog(context: Context) { fun setCustomButton(textId: Int, listener: (() -> Unit)?): NumberPickerDialog { builder.setNeutralButton(textId) { _, _ -> - numberPicker?.let { - it.clearFocus() - it.hideSoftInput() - listener?.invoke() - } + numberPicker?.clearFocus() + listener?.invoke() } return this } fun show(callBack: ((value: Int) -> Unit)?) { - builder.setPositiveButton(R.string.ok) { _, _ -> - numberPicker?.let { - it.clearFocus() - it.hideSoftInput() - callBack?.invoke(it.value) - } - } + builder.setPositiveButton(R.string.ok, null) builder.setNegativeButton(R.string.cancel, null) val dialog = builder.show() + numberPicker = dialog.findViewById(R.id.number_picker) + inputLayout = dialog.findViewById(R.id.til_input) + editText = dialog.findViewById(R.id.et_input) + tvRange = dialog.findViewById(R.id.tv_range) + val btnSwitch = dialog.findViewById(R.id.btn_switch_input) + numberPicker?.let { np -> - minValue?.let { - np.minValue = it + minValue?.let { np.minValue = it } + maxValue?.let { np.maxValue = it } + value?.let { np.value = it } + } + tvRange?.text = "输入范围: ${numberPicker?.minValue} - ${numberPicker?.maxValue}" + + btnSwitch?.setOnClickListener { + useInputMode = !useInputMode + if (useInputMode) { + numberPicker?.visibility = View.GONE + inputLayout?.visibility = View.VISIBLE + tvRange?.visibility = View.VISIBLE + editText?.setText(numberPicker?.value?.toString() ?: value?.toString()) + } else { + numberPicker?.visibility = View.VISIBLE + inputLayout?.visibility = View.GONE + tvRange?.visibility = View.GONE + inputLayout?.error = null } - maxValue?.let { - np.maxValue = it - } - value?.let { - np.value = it + } + + dialog.getButton(AlertDialog.BUTTON_POSITIVE)?.setOnClickListener { + if (useInputMode) { + val num = editText?.text?.toString()?.toIntOrNull() + val min = minValue ?: numberPicker?.minValue ?: Int.MIN_VALUE + val max = maxValue ?: numberPicker?.maxValue ?: Int.MAX_VALUE + if (num == null || num !in min..max) { + inputLayout?.error = "请输入 $min - $max 范围内的数字" + } else { + inputLayout?.error = null + callBack?.invoke(num) + dialog.dismiss() + } + } else { + numberPicker?.let { + it.clearFocus() + callBack?.invoke(it.value) + dialog.dismiss() + } } } } -} \ No newline at end of file +} diff --git a/app/src/main/res/layout/dialog_number_picker.xml b/app/src/main/res/layout/dialog_number_picker.xml index f54fd0a00..c27b2d56e 100644 --- a/app/src/main/res/layout/dialog_number_picker.xml +++ b/app/src/main/res/layout/dialog_number_picker.xml @@ -1,13 +1,55 @@ - + android:layout_height="match_parent" + android:padding="24dp" + android:gravity="center"> + android:scrollbars="none" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="@id/btn_switch_input" + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintBottom_toBottomOf="parent"/> - \ No newline at end of file + + + + + + + + +