Вот как выглядит конкретный XML-код, на который я пытаюсь настроить таргетинг: он имеет относительную компоновку и является частью фрагмента
Код: Выделить всё
Код: Выделить всё
class RegisterFragment : Fragment() {
private lateinit var binding: FragmentRegisterBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentRegisterBinding.inflate(inflater, container, false)
val tfInterest = binding.tfInterest
tfInterest.filters = arrayOf(NumberRangeFilter())
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_register, container, false)
}
class NumberRangeFilter : InputFilter {
override fun filter(
source: CharSequence?,
start: Int,
end: Int,
dest: Spanned?,
dstart: Int,
dend: Int
): CharSequence? {
val input = source.toString()
val value = try {
Integer.parseInt(input)
} catch (e: NumberFormatException) {
return ""
}
// Check if the input is within the range of 1 to 100
return if (value in 1..100) {
// Check if the resulting text will be within the range of 1 to 100 after replacement
val newValue = dest.toString().substring(0, dstart) + input + dest.toString().substring(dend)
val newValueInt = try {
Integer.parseInt(newValue)
} catch (e: NumberFormatException) {
return ""
}
if (newValueInt in 1..100) {
null // Accept input
} else {
"" // Reject input
}
} else {
"" // Reject input if it's outside the range
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... a-edittext
Мобильная версия