2017-10-17 44 views
0

我正在創建具有必填字段(如名和手機號碼)的登錄頁面,因爲我還使用XMl設計了浮動標籤,但浮動標籤未應用於必填字段,而是應用於普通字段我把hint.Below是我用於浮點代碼labels.Can誰能幫我解決這個登錄頁面中必填字段的浮動標籤

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

       <EditText 
        android:id="@+id/mobilenumber" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="textPassword" 
        android:textSize="15dp"/> 
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 
      </android.support.design.widget.TextInputLayout> 

回答

0

您可以參考下面這個例子:

首先,你要的gradle中添加代碼:

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:23.0.1' 
    compile 'com.android.support:design:23.0.1' 
} 

和佈局

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 
    </android.support.design.widget.AppBarLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="?attr/actionBarSize" 
     android:orientation="vertical" 
     android:paddingLeft="20dp" 
     android:paddingRight="20dp" 
     android:paddingTop="60dp"> 

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

      <EditText 
       android:id="@+id/input_name" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:singleLine="true" 
       android:hint="@string/hint_name" /> 
     </android.support.design.widget.TextInputLayout> 

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

      <EditText 
       android:id="@+id/input_email" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:inputType="textEmailAddress" 
       android:hint="@string/hint_email" /> 
     </android.support.design.widget.TextInputLayout> 

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

      <EditText 
       android:id="@+id/input_password" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:inputType="textPassword" 
       android:hint="@string/hint_password" /> 
     </android.support.design.widget.TextInputLayout> 

     <Button android:id="@+id/btn_signup" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/btn_sign_up" 
      android:background="@color/colorPrimary" 
      android:layout_marginTop="40dp" 
      android:textColor="@android:color/white"/> 

    </LinearLayout> 

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

我希望它可以幫助您的問題!

您可以參考更多:https://www.androidhive.info/2015/09/android-material-design-floating-labels-for-edittext/

0

在你的build.gradle添加最新的程序兼容性和設計庫。

dependencies 
{ 
compile 'com.android.support:appcompat-v7:X.X.X' // where X.X.X version 
compile 'com.android.support:design:X.X.X' // where X.X.X version 
} 

聲明你的EditText與TextInputLayout

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

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/Title" /> 

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

注:

兩個部件TextInputLayout和EditText上有機器人:hint屬性,你可以使用其中任何一個。如果您的應用支持橫向模式,請使用TextInputEditText替換EditText以便提示正確顯示。

+0

我用的是相同的,但在編輯文本我想強制性標誌即紅色的開始,但這是可能的暗示。 –

+0

我不這麼認爲,你可以做的是setError()如果edittext爲空 –

+0

如果你想顯示一個固定的紅色*,那麼解決方法是使用FrameLayout來包裝TextInputLayout和TextView。將TextView左對齊,並將LeftPadding添加到EditText,以便輸入的文本將出現在* –