我有一个带有多个TextInputLayouts的xml.其中之一如下:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textInputEmail"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginStart="@dimen/default_margin"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="@dimen/default_margin"
android:layout_marginRight="@dimen/default_margin"
android:layout_marginTop="@dimen/default_margin_half"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginBottom="@dimen/default_margin_half"
app:layout_constraintBottom_toBottomOf="parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email"
android:text="@={viewmodel.username}"
android:onTextChanged="@{(text, start, before, count) -> viewmodel.onTextChanged(text)}"
android:inputType="textEmailAddress"/>
</android.support.design.widget.TextInputLayout>
在我的视图模型中,我实现了文本更改侦听器,如下所示:
fun onTextChanged(text: CharSequence){
Log.i("Loginviewmodel", "username = "+text)
}
我想做的是这样的:
fun onTextChanged(text: CharSequence, view: View){
when(view.id){
R.id.editText1 -> doSomething1()
R.id.editText2 -> doSomething2()
}
当使用数据绑定调用方法时,是否可以传递触发该方法的视图的view / id / reference?
解决方法:
是的,您可以在数据绑定中传递视图,只需在视图模型中使用View参数创建方法:
fun onTextChanged(text: CharSequence, view: View){
when(view.id) {
R.id.editText1 -> doSomething1()
R.id.editText2 -> doSomething2()
}
}
然后将视图的ID传递给XML布局中的方法:
<android.support.design.widget.TextInputEditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email"
android:text="@={viewmodel.username}"
android:onTextChanged="@{(text, start, before, count) -> viewmodel.onTextChanged(text, editTextEmail)}"
android:inputType="textEmailAddress"/>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。