0

我有以下布局 - 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>

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

谢谢。

4

1 回答 1

2

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;
于 2018-07-31T10:30:01.357 回答