Android EditText TextWatcher替换为LiveData

Android EditText TextWatcher替换为LiveData,android,android-edittext,android-databinding,android-livedata,Android,Android Edittext,Android Databinding,Android Livedata,有没有办法用双向数据绑定模式取代EditTextTextWatcher 我试过: class AddViewModel @Inject constructor() : ViewModel() { val description = MutableLiveData<String>() } class AddViewModel@Inject构造函数():ViewModel(){ val description=MutableLiveData() } 我发现了一些类似的东西

有没有办法用双向数据绑定模式取代
EditText
TextWatcher

我试过:

class AddViewModel @Inject constructor() : ViewModel() {
    val description = MutableLiveData<String>()
}
class AddViewModel@Inject构造函数():ViewModel(){
val description=MutableLiveData()
}

我发现了一些类似的东西,但不知怎么的,我不能让它工作

更新:

我只是在
@


android:text=“@{viewModel.description}”
->
android:text=“@={viewModel.description}”

您应该在XML中使用OnTextChanged属性

 <data>

    <variable
        name="onTextChanged"
        type="androidx.databinding.adapters.TextViewBindingAdapter.OnTextChanged" />
 </data>
 <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/descriptionEditText"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="@dimen/default_spacing"
            android:background="@drawable/ic_input_background"
            android:gravity="top"
            android:minHeight="200dp"
            android:padding="4dp"
            android:onTextChanged="@{viewModel.description}"
            app:layout_constraintBottom_toTopOf="@+id/postButton"
            app:layout_constraintEnd_toEndOf="parent"/>

它提供了
未知属性android:onTextChanged
error您需要添加数据绑定适配器。再次检查此项我接受您的答案,但我缺少的关键要素是,如果我使用双向数据绑定,我实际上根本不需要onChangeListener。我缺少的关键是
=
@
之后签名,它应该是这样的
android:text=“@={viewModel.description}”
 <data>

    <variable
        name="onTextChanged"
        type="androidx.databinding.adapters.TextViewBindingAdapter.OnTextChanged" />
 </data>
 <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/descriptionEditText"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="@dimen/default_spacing"
            android:background="@drawable/ic_input_background"
            android:gravity="top"
            android:minHeight="200dp"
            android:padding="4dp"
            android:onTextChanged="@{viewModel.description}"
            app:layout_constraintBottom_toTopOf="@+id/postButton"
            app:layout_constraintEnd_toEndOf="parent"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <include
        layout="@layout/base_edittext_view"
        app:onTextChanged="@{(text, start, before, count) -> viewModel.onTextChanged(text)}"/>

</LinearLayout>
fun description(s: CharSequence,start: Int,before : Int,count :Int){
    //TODO write your implementation here ...
   }