繁体   English   中英

Android:数据绑定:在包含的布局中提供值

[英]Android: Data Binding: providing values in included layouts

我有以下布局-text_input_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="textType" type="String"/>
    </data>
    <TextInputLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        app:errorEnabled="true"
      android:textColorHint="@color/white"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <android.support.design.widget.TextInputEditText
            android:textColor="@color/White"
            android:backgroundTint="@color/White"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:inputType="@{textType}"
            android:id="@+id/password" />
    </TextInputLayout>
</layout>

我想将此布局包含在另一个布局中,但根据需要将inputType更改为textPassword,textEmail等。

<layout xmlns:bind="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <include layout="@layout/text_input_layout"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
app:textType="textPassword"/>

    </RelativeLayout>
</layout>

我尝试使用数据绑定,但是它似乎不起作用。 有人可以帮忙吗?

谢谢。

textPassword不是字符串。 这是一个attribute因此您不能像app:textType="textPassword"那样传递它。 app:textType接受整数值。

1)将textType作为Integer数值。

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="textType"
            type="Integer"/>
    </data>

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <android.support.design.widget.TextInputEditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="@{textType}"
            />
    </android.support.design.widget.TextInputLayout>
</layout>

2)将Integer传递给<include标签app:textType

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <import type="android.text.InputType"/>

    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <include
            layout="@layout/sample"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:textType="@{(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)}"
            />

    </android.support.constraint.ConstraintLayout>
</layout>

您也可以传递1,2,3 ...而不导入InputType但这应该是有效的InputType

int TYPE_CLASS_TEXT = 1;
int TYPE_TEXT_VARIATION_PASSWORD = 128;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM