How to databind to onTextChanged for an EditText on Android?

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

    Actually it works out of the box. I think my mistake was using an old version of the data binding framework. Using the latest, this is the procedure:

    View:

    
    

    Model:

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.w("tag", "onTextChanged " + s);
    }
    

    Make also sure that you have assigned model into DataBinding

    For ex. in your activity

    lateinit var activityMainDataBinding: ActivityMainBinding
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityMainDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main) 
        val dataViewModel = ViewModelProvider(this).get(DataViewModel::class.java)
        activityMainDataBinding.dataModel = dataViewModel
    }
    

    Make sure you are referncing gradle build tools v1.5.0 or higher and have enabled databinding with android.dataBinding.enabled true in your build.gradle.

    edit: Functioning demo project here. view. model.

提交回复
热议问题