代码之家  ›  专栏  ›  技术社区  ›  Farrokh Shahriari

如何在Android中通过数据绑定将ContextChanged侦听器添加到包含布局XML

  •  1
  • Farrokh Shahriari  · 技术社区  · 7 年前

    我有一个问题要将ContextChanged添加到 包括布局 .

    有一个基本的_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>
    

    我要将ContextChanged侦听器添加到包含base_edittext_view.xml的布局中。

    android:onTextChanged="@{(text, start, before, count) -> viewModel.onTextChanged(text)}"
    

    添加onclick到没有问题 包括布局 但对于ContextChanegd,我不知道如何实现它。

    注:

         <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"
                    />
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Khemraj Sharma    7 年前

    你可以路过 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}" />
    

    现在你可以通过了 文本更改 包括如下布局。

    <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>
    

    ------------就是这样!

    有很多其他方法可以实现它。我想和你分享完整的信息。

    2路

    您可以在父布局中创建另一个数据变量。并将其包含到上面的包含布局中。如下所示

    <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>
    

    然后你就可以实现 OnTextChanged 在活动/视图模型中(无论您需要在哪里)。

    binding.setOnTextChanged(new TextViewBindingAdapter.OnTextChanged() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // do you work.
        }
    });
    

    3路

    或者如果不想在父布局中使用其他变量, 然后你可以直接通过 文本更改 从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中创建自定义方法并从布局中调用该方法。