我面临一个怪异的问题,其中有一个InputTextLayout和一个EditText,并且我正在尝试实现类似以下内容(如下图所示)(来自 Material 设计指南的图像:https://material.io/guidelines/components/text-fields.html#text-fields-layout),其中有两个单独的提示。我这样做是通过将android:hint添加到这两种布局中来的。这可以正常工作,但是当焦点移开该位置时,“标签”将向下移动并与“占位符”文本重叠。 (仅当用户未提供任何输入且编辑文本为空时,两个提示才重叠)。关于如何解决此问题的任何指示?

作为一项要求,我需要两个提示都存在,并且“标签”不应向下移至焦点更改。两种提示都应保持在原位

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

        <android.support.v7.widget.AppCompatEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Placeholder"
            android:inputType="textCapWords"
            />

    </android.support.design.widget.TextInputLayout>

android - Android TextInputLayout提示与EditText提示重叠-LMLPHP

最佳答案

据我所知,我们不能如你所愿。
因为,TextInputLayout旨在使提示一旦聚焦就 float 该提示。因此,一旦提示升高,占位符将不存在。我们可以通过以下一些细微改动来满足您的要求。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    android:gravity="center"
    tools:context="com.stackoverflow.MainActivity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:text="Label"
        android:textColor="@color/colorPrimary" />

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:paddingLeft="10dp"
        app:hintEnabled="false">

        <android.support.v7.widget.AppCompatEditText
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:hint="Place Holder"
            />

    </android.support.design.widget.TextInputLayout>

</RelativeLayout>
android - Android TextInputLayout提示与EditText提示重叠-LMLPHP

10-07 12:30
查看更多