的Android TextInputLayout暗示重叠的EditText暗示

问题描述:

我面临的一个奇怪的问题,我有一个InputTextLayout,并在其中一个EditText,我想实现这样的事情(在下面的图片所示)(图片由材料设计guidlines:https://material.io/guidelines/components/text-fields.html#text-fields-layout) ,那里有两个单独的提示。我通过将android:提示添加到两个布局来实现此目的。这很好,但当焦点离开这个时候,“标签”向下移动并重叠“占位符”文本。 (只有当用户没有给出任何输入并且编辑文本为空时 - 两个提示都重叠)。任何关于如何解决这个问题的指针?的Android TextInputLayout暗示重叠的EditText暗示

作为一项要求,我需要两个提示,并且“标签”不应该因焦点更改而下移。这两种提示应该留在自己的位置

<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> 

enter image description here

+0

从AppCompatEditText中删除提示 – nitinkumarp

+0

从AppcompatEditText或TextInputLayout中删除提示。你会留在那里的一个将会填充你的提示。 –

+0

@PallaviTapkir,这将工作,但作为一个要求,我需要两个提示在那里,其中一个应该隐藏焦点移动。或者如果有任何方法,“标签”不应该下移 –

删除提示,我知道,我们不能按照你的意愿去做。

因为TextInputLayout被设计为一旦获得焦点就浮动提示所以,一旦它上升,没有东西会在占位符中出现。我们可以按照以下的细微变化来满足您的要求。

<?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.*.MainActivity"> 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="10dp" 
     android:text="Lable" 
     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> 

enter image description here

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

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

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

使用下面的代码。它应该工作正常。

<android.support.design.widget.TextInputLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="8dp" 
      android:layout_marginTop="8dp" 
      android:textColorHint="@color/white"> 

      <EditText 

       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:hint="Placeholder" 
       android:inputType="text" 
       android:textColor="@color/white" 
       android:textColorHint="@color/white" /> 
     </android.support.design.widget.TextInputLayout> 

无论从AppcompatEditTextTextInputLayout

使用android:hint=""只在一个或者AppcompatEditTextTextInputLayout

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

    <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> 

了解更多信息 check this link

完美一个!

enter image description here

XML代码

<android.support.design.widget.TextInputLayout 
     android:id="@+id/inputLayoutPhone" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="24dp" 
     android:hint="Phone Number"> 

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

科特林代码

edtPhone.hint="" 
edtPhone.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus -> 
     inputLayoutPhone.isHintEnabled = hasFocus 
     if(hasFocus){ 
      edtPhone.hint="+91" 
     }else{ 
      edtPhone.hint= "Phone Number" 
     } 
    } 

Java代码

edtPhone.setHint(""); 
    edtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View view, boolean hasFocus) { 
      if (hasFocus){ 
       edtPhone.setHint("+91"); 
      }else{ 
       edtPhone.setHint("Phone Number"); 
      } 

     } 
    });