如何减少代码重复:TextInputLayout

How to reduce code repetition: TextInputLayout

我刚刚开始使用 TextInputLayout 在 android 中启用浮动提示。以下是我的做法:

<android.support.design.widget.TextInputLayout
    android:id="@+id/text_input_layout_password"
    style="@style/TextInputLayout"
    android:layout_below="@+id/line1">

    <EditText
        style="@style/EditText"
        android:id="@+id/password"
        android:hint="Password"
        android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>

问题就出在这里。我必须为所有 EditText 字段编写相同的 TextInputLayout。我有一个包含 7 个字段的注册 activity。有没有办法一次性实现所有 EditTexts 的 TextInputLayout?还是要写七遍?

您要么必须管理 Java 文件中的所有内容,要么必须将其放入 XML.

中 7 次

如果您有不同的属性值,例如 inputTypehint,那么最好在 XML 中使用,否则您必须管理 Java

中的所有内容

Note: If you have same type of all TextInputLayout then you can create one common XML for it and use <include> to include in you main xml.

谢谢。