问题
Okay, so I have a ViewModel
with a getter getTitle()
that returns MutableLiveData<String>
.
<EditText
style="@style/Widget.EditText.FullWidth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/label_title"
android:inputType="text"
android:text="@={ viewModel.title }" />
This works fine at first: the EditText
contains the value of the MutableLiveData
when it first appears. However, if the value of this data is updated using MutableLiveData.setValue()
(such as by another EditText
, or from my code), then the value inside of the text box does not change. How do I fix this?
回答1:
This works properly in the new version of Android Studio, which supports binding to LiveData
objects properly.
来源:https://stackoverflow.com/questions/49723950/edittext-livedata-two-way-binding