2015-04-23 34 views
1

在我的android應用程序中,我的EditText視圖中有一條提示文字。一旦用戶開始輸入,我想讓提示顯示在edittext字段的頂部。當用戶開始輸入時,原來的提示文字就會消失,這很好。如何在edittext字段上放置文本?有沒有辦法讓EditText字段多行,並使用提示的第一行?但用戶輸入應始終從第二行開始。 enter image description here如何在android EditText字段中保留提示文本?

在此先感謝您的幫助!

+0

添加上面的文本視圖,並將其設置爲消失或不可見。當用戶開始輸入時,將電視可見性設置爲可見 – jb15613

回答

0

您可以使用FloatLabelLayout。這是一個自定義組件,按照谷歌文檔中的描述工作。

一旦你創建你可以以這種方式使用它的自定義組件:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <your.package.FloatLabelLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:floatLabelTextAppearance="@style/TextAppearance.YourApp.FloatLabel"> 

     <EditText 
      android:id="@+id/edit_username" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/account_username_hint" 
      android:singleLine="true" 
      android:inputType="textNoSuggestions" 
      android:imeOptions="actionNext" 
      android:nextFocusDown="@+id/edit_password" /> 

    </your.package.FloatLabelLayout> 

    <your.package.FloatLabelLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:floatLabelTextAppearance="@style/TextAppearance.YourApp.FloatLabel"> 

     <EditText 
      android:id="@+id/edit_password" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/account_password_hint" 
      android:singleLine="true" 
      android:inputType="textNoSuggestions" 
      android:imeOptions="actionDone" /> 

    </your.package.FloatLabelLayout> 

</LinearLayout> 
1

有新的widget由Android稱爲TextInputLayout提供它的標準方式。下面是一個例子:

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

     <EditText 
      android:id="@+id/first_edit_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="hello"/> 
    </android.support.design.widget.TextInputLayout> 
+0

輸入文本時此答案存在問題,提示消失。我正在使用26.0.2 –

相關問題