我在将 onTextChanged 添加到 include 布局 中的 TextInputEditText 时遇到问题。
有一个base_edittext_view.xml如下:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/colorPrimaryText"
android:background="@drawable/textinputlayout_background"
app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout">
<android.support.design.widget.TextInputEditText
android:id="@+id/edittextItem"
style="@style/TextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/input_border_bottom"
android:cursorVisible="true"
android:drawableEnd="@drawable/ic_user"
android:drawablePadding="@dimen/padding_medium"
android:gravity="center|left"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:maxLength="50"
android:paddingBottom="@dimen/padding_medium"
android:paddingTop="@dimen/padding_medium"
android:paddingEnd="@dimen/padding_xlarge"
android:paddingStart="@dimen/padding_medium"
android:textColor="@color/edittext_text_color"
android:textColorHint="@color/edittext_hint_text_color"
android:textSize="@dimen/textsize_medium"
/>
<!---->
</android.support.design.widget.TextInputLayout>
我想将 onTextChanged 监听器添加到包含 base_edittext_view.xml 的布局。
android:onTextChanged="@{(text, start, before, count) -> viewModel.onTextChanged(text)}"
在include layout中添加onClick没有问题,但是对于onTextChanegd我不知道如何实现。
注意:
<include
android:id="@+id/edittextEmail"
layout="@layout/edittext_email_base_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/padding_xxxxlarge"
/>
最佳答案
您可以通过将 onTextChanged
传递给包含的布局来实现。参见示例 -
这是base_edittext_view.xml
<data>
<variable
name="onTextChanged"
type="androidx.databinding.adapters.TextViewBindingAdapter.OnTextChanged" />
</data>
<com.google.android.material.textfield.TextInputEditText
...
android:onTextChanged="@{onTextChanged}" />
现在您可以将 onTextChanged
传递给包含的布局,如下所示。
<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>
------------就是这样!
可以有许多其他方法来实现它。我想与您分享完整的信息。
方法二
您可以在父布局中创建另一个数据变量。并将其包含在上面的布局中。就像下面-
<data>
<variable
name="onTextChanged"
type="androidx.databinding.adapters.TextViewBindingAdapter.OnTextChanged" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
layout="@layout/base_edittext_view"
app:onTextChanged="@{onTextChanged}" />
</LinearLayout>
然后您可以在 Activity/ViewModel(任何您需要的地方)中实现 OnTextChanged
。
binding.setOnTextChanged(new TextViewBindingAdapter.OnTextChanged() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// do you work.
}
});
方式三
或者如果您不想在父布局中使用另一个变量,那么您可以直接将 onTextChanged
传递给来自 Java/Kotlin 类的包含布局。参见示例-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/includedEditText"
layout="@layout/base_edittext_view"
/>
</LinearLayout>
然后从 Java 类-
binding.includedEditText.setOnTextChanged(new TextViewBindingAdapter.OnTextChanged() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// your work
}
});
还有很多其他的方法。就像在 ViewModel/Activity 中创建自定义方法并从布局中调用该方法。
关于android - 如何通过 android 中的数据绑定(bind)将 onTextChanged 监听器添加到包含布局 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54335811/