How to databind to onTextChanged for an EditText on Android?

后端 未结 9 1184
悲哀的现实
悲哀的现实 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:25

    Im using this method to handle on text change listener in android databinding.1st in your ViewModel class create LiveData variable, And also create getText method which returns LiveData object.

    • public MutableLiveData verifyCodes = new MutableLiveData<>();
    • public LiveData getCodes(){ return verifyCodes; }

    Then in your xml files editText field set attribute on text bind with above liveData field

    In databinding you already know how to create variable of viewModel class inside data tag i beleive.As example


    Ok now your activity you have to observe liveData object we created inside viewModel class

    • mViewModel.getCodes().observe(this,new Observer< String>(){ @Override public void onChange(String strings){ log.d("OnChange",strings); }});

    You can perform any logic when text changing inside onChange method

提交回复
热议问题