android - 如何通过 android 中的数据绑定(bind)将 onTextChanged 监听器添加到包含布局 xml

标签 android android-layout android-databinding addtextchangedlistener android-include

我在将 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/

相关文章:

android - 禁用某些布局的数据绑定(bind)

android - 绑定(bind)适配器中的命名空间?

android - 加载设备灰显 (Android Studio) 在物理设备上测试应用程序

android - 播放 Sdcard 中的音频文件

android - 将 sqlcipher 从 3.5.2 升级到 4.0.1 后,GreenDAO 无法访问现有数据库

Android:以编程方式在表格中创建两列

android - 使用 Sqlite Cursor 时如何让对话框中的 setMultiChoiceItems 复选框更新

包名中的 Android Studio Manifest 合并错误

android - 如何自定义抽屉导航项目高度

android - 如何使用数据绑定(bind)创建焦点更改监听器