mihon/app/src/main/java/eu/kanade/tachiyomi/widget/MinMaxNumberPicker.kt

41 lines
1.4 KiB
Kotlin
Raw Normal View History

2016-03-08 01:22:56 +01:00
package eu.kanade.tachiyomi.widget
import android.content.Context
import android.text.InputType
2016-03-08 01:22:56 +01:00
import android.util.AttributeSet
import android.widget.EditText
2016-03-08 01:22:56 +01:00
import android.widget.NumberPicker
import androidx.core.view.doOnLayout
2016-03-08 01:22:56 +01:00
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.view.findDescendant
2016-03-08 01:22:56 +01:00
class MinMaxNumberPicker @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
2020-04-25 20:24:45 +02:00
NumberPicker(context, attrs) {
2016-03-08 01:22:56 +01:00
override fun setDisplayedValues(displayedValues: Array<out String>?) {
super.setDisplayedValues(displayedValues)
// Disable keyboard input when a value that can't be auto-filled with number exists
val notNumberValue = displayedValues?.find { it.getOrNull(0)?.digitToIntOrNull() == null }
if (notNumberValue != null) {
descendantFocusability = FOCUS_BLOCK_DESCENDANTS
}
}
2016-03-08 01:22:56 +01:00
init {
if (attrs != null) {
val ta = context.obtainStyledAttributes(attrs, R.styleable.MinMaxNumberPicker, 0, 0)
try {
minValue = ta.getInt(R.styleable.MinMaxNumberPicker_min, 0)
maxValue = ta.getInt(R.styleable.MinMaxNumberPicker_max, 0)
} finally {
ta.recycle()
}
}
doOnLayout {
findDescendant<EditText>()?.setRawInputType(InputType.TYPE_CLASS_NUMBER)
}
2016-03-08 01:22:56 +01:00
}
}