inline fun EditText.onTextChanged(crossinline textChangeListener: (String) -> Unit): TextWatcher { val textWatcher = object : TextWatcher { override fun afterTextChanged(s: Editable?) { } override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { } override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { textChangeListener(s.toString()) } } this.addTextChangedListener(textWatcher) return textWatcher } Now I can do editText.onTextChanged { viewModel.onTextChanged(it) }
@HugoVisser4 жыл бұрын
Not sure what you are trying to point out, but you can obviously call that code from a binding adapter too if you like. In both cases you need to write that code, either as an extension function, in a binding adapter or a combination of both. Then if you can just use that in your view as if Android had this already implemented that on EditText. There are other ways to track changes to texts too by the way. You can use a MutableLiveData too for example. In that case there's no binding adapter to write, you just bind that MutableLiveData in your view model to your view.
@Zhuinden4 жыл бұрын
@@HugoVisser Just that if the issue is that you want a method to exist but instead you get this TextWatcher with a bloated interface, Kotlin extension functions can also solve that issue
@Zhuinden4 жыл бұрын
Apparently it's also in Ktx, except it's called `doOnTextChanged`.
@coreflodev4 жыл бұрын
that's terrible code, why are people writing so much code? A textView is enough to implement the warning/error/info component