mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-07 01:28:52 -06:00
Focus fractional part after entering a decimal separator
This commit is contained in:
@@ -19,9 +19,11 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.activities.common.dialogs
|
package org.isoron.uhabits.activities.common.dialogs
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.DialogInterface
|
import android.content.DialogInterface
|
||||||
import android.text.InputFilter
|
import android.text.InputFilter
|
||||||
|
import android.text.Spanned
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
|
import android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
|
||||||
import android.view.inputmethod.EditorInfo
|
import android.view.inputmethod.EditorInfo
|
||||||
@@ -41,18 +43,24 @@ class NumberPickerFactory
|
|||||||
@Inject constructor(
|
@Inject constructor(
|
||||||
@ActivityContext private val context: Context
|
@ActivityContext private val context: Context
|
||||||
) {
|
) {
|
||||||
|
|
||||||
fun create(
|
fun create(
|
||||||
value: Double,
|
value: Double,
|
||||||
unit: String,
|
unit: String,
|
||||||
callback: ListHabitsBehavior.NumberPickerCallback
|
callback: ListHabitsBehavior.NumberPickerCallback
|
||||||
): AlertDialog {
|
): AlertDialog {
|
||||||
|
|
||||||
val inflater = LayoutInflater.from(context)
|
val inflater = LayoutInflater.from(context)
|
||||||
val view = inflater.inflate(R.layout.number_picker_dialog, null)
|
val view = inflater.inflate(R.layout.number_picker_dialog, null)
|
||||||
|
|
||||||
val picker = view.findViewById<NumberPicker>(R.id.picker)
|
val picker = view.findViewById<NumberPicker>(R.id.picker)
|
||||||
val picker2 = view.findViewById<NumberPicker>(R.id.picker2)
|
val picker2 = view.findViewById<NumberPicker>(R.id.picker2)
|
||||||
|
|
||||||
|
val watcherFilter: InputFilter = SeparatorWatcherInputFilter(picker2)
|
||||||
|
val numberPickerInputText = getNumberPickerInputText(picker)
|
||||||
|
|
||||||
|
// watch the unfiltered input before the filters remove a possible separator from it
|
||||||
|
numberPickerInputText.filters = arrayOf(watcherFilter).plus(numberPickerInputText.filters)
|
||||||
|
|
||||||
view.findViewById<TextView>(R.id.tvUnit).text = unit
|
view.findViewById<TextView>(R.id.tvUnit).text = unit
|
||||||
view.findViewById<TextView>(R.id.tvSeparator).text =
|
view.findViewById<TextView>(R.id.tvSeparator).text =
|
||||||
DecimalFormatSymbols.getInstance().decimalSeparator.toString()
|
DecimalFormatSymbols.getInstance().decimalSeparator.toString()
|
||||||
@@ -68,7 +76,6 @@ class NumberPickerFactory
|
|||||||
picker2.maxValue = 99
|
picker2.maxValue = 99
|
||||||
picker2.setFormatter { v -> String.format("%02d", v) }
|
picker2.setFormatter { v -> String.format("%02d", v) }
|
||||||
picker2.value = intValue % 100
|
picker2.value = intValue % 100
|
||||||
refreshInitialValue(picker2)
|
|
||||||
|
|
||||||
val dialog = AlertDialog.Builder(context)
|
val dialog = AlertDialog.Builder(context)
|
||||||
.setView(view)
|
.setView(view)
|
||||||
@@ -91,20 +98,50 @@ class NumberPickerFactory
|
|||||||
InterfaceUtils.setupEditorAction(
|
InterfaceUtils.setupEditorAction(
|
||||||
picker
|
picker
|
||||||
) { _, actionId, _ ->
|
) { _, actionId, _ ->
|
||||||
if (actionId == EditorInfo.IME_ACTION_DONE)
|
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
||||||
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick()
|
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick()
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
InterfaceUtils.setupEditorAction(
|
||||||
|
picker2
|
||||||
|
) { _, actionId, _ ->
|
||||||
|
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
||||||
|
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick()
|
||||||
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
return dialog
|
return dialog
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun refreshInitialValue(picker: NumberPicker) {
|
@SuppressLint("DiscouragedPrivateApi")
|
||||||
// Workaround for Android bug:
|
private fun getNumberPickerInputText(picker: NumberPicker): EditText {
|
||||||
// https://code.google.com/p/android/issues/detail?id=35482
|
|
||||||
val f = NumberPicker::class.java.getDeclaredField("mInputText")
|
val f = NumberPicker::class.java.getDeclaredField("mInputText")
|
||||||
f.isAccessible = true
|
f.isAccessible = true
|
||||||
val inputText = f.get(picker) as EditText
|
return f.get(picker) as EditText
|
||||||
inputText.filters = arrayOfNulls<InputFilter>(0)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SeparatorWatcherInputFilter(private val nextPicker: NumberPicker) : InputFilter {
|
||||||
|
override fun filter(
|
||||||
|
source: CharSequence?,
|
||||||
|
start: Int,
|
||||||
|
end: Int,
|
||||||
|
dest: Spanned?,
|
||||||
|
dstart: Int,
|
||||||
|
dend: Int
|
||||||
|
): CharSequence {
|
||||||
|
if (source == null || source.isEmpty()) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
for (c in source) {
|
||||||
|
if (c == DecimalFormatSymbols.getInstance().decimalSeparator || c == '.' || c == ',') {
|
||||||
|
nextPicker.performLongClick()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return source
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user