How to set error on EditText using DataBinding Framework MVVM

前端 未结 4 1826
旧巷少年郎
旧巷少年郎 2021-02-19 09:26

I am using Android Data Binding framework I have suppose an EditText for login form with username as below



        
4条回答
  •  失恋的感觉
    2021-02-19 09:58

    Fundamentally, you need a way to implement dependent fields. Error is dependent on the value of text. You want error value to get updated when text changes.

    I have found two ways to achieve this:

    Set attribute using Data Binding expression

    Problem with EditText

    EditText clears the error when user types. Data Binding expects that attribute's value is retained after invoking setter. So, it does not invoke the setter again if the value doesn't change. Hence, as soon as you type, if the computed error value is same, data binding will not call setter and hence, the error will disappear. This kind of makes error attribute incompatible with Data Binding.

    I prefer to use TextInputLayout provided by design library. It has a persistent error field and also looks better.

提交回复
热议问题