TextInputLayout 的不同标签和提示

新手上路,请多包涵

我想制作一个 EditTextLayout ,但我想要一个不同的标签和提示文本。

例如:标签文本为“电话号码”,提示文本为“+6281342134”。

可能吗?

我的代码是:

     <android.support.design.widget.TextInputLayout
        android:id="@+id/phone_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="+6281342134"
            android:inputType="phone" />
    </android.support.design.widget.TextInputLayout>

原文由 mutokenji 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 438
2 个回答

您可以使用这些属性:

  • android:hint :用于标签

  • placeholderText :用于 EditText 中的占位符文本

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/phone_layout"
            android:layout_width="match_parent"
            android:hint="Phone Number"
            app:placeholderText="+6281342134"
            android:layout_height="wrap_content">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/phone"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="+6281342134"
                android:inputType="phone" />

        </com.google.android.material.textfield.TextInputLayout>

在此处输入图像描述

如果在文本字段为空时也应显示占位符,只需添加 expandedHintEnabled=“false”

         <com.google.android.material.textfield.TextInputLayout
            app:expandedHintEnabled="false"

在此处输入图像描述

注意: expandedHintEnabled 至少需要版本 1.3.0-alpha03

原文由 Gabriele Mariotti 发布,翻译遵循 CC BY-SA 4.0 许可协议

我对热汗采取了类似的方法

<android.support.design.widget.TextInputEditText
    android:id="@+id/edit_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

 editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            inputLayout.setHint("We did it!");
        } else {
            inputLayout.setHint("Testing 123");
        }
    }
});

在不禁用动画的情况下,动画对我来说已经足够好了:

例子

原文由 starkej2 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题