How to databind to onTextChanged for an EditText on Android?

后端 未结 9 1188
悲哀的现实
悲哀的现实 2020-11-30 01:41

In Yigit Boyar and George Mount\'s talk on Android Databinding they illustrate how easy it is to bind to TextWatcher\'s onTextChanged (at 13:41). O

9条回答
  •  悲哀的现实
    2020-11-30 02:26

    If you just need text parameter after text has changed, you could use android:afterTextChanged binding adapter. for example:

    android:afterTextChanged="@{(text) -> viewModel.onTextChange(text)}"
    

    Then in your ViewModel just implement like this:

    fun onTextChange(editable: Editable?) {
        Log.d("TAG","New text: ${editable.toString()}")
    }
    

    Furthermore, there is android:beforeTextChanged which used to know old text before text change event, usage is same as android:afterTextChanged.

提交回复
热议问题