Android EditText view Floating Hint in Material Design

前端 未结 10 2139
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 08:13

Does API 21 provide a method to use the following feature:

http://www.google.com/design/spec/components/text-fields.html#text-fields-floating-labels

I\'m try

相关标签:
10条回答
  • 2020-12-02 08:58

    The Android support library can be imported within gradle in the dependencies :

        compile 'com.android.support:design:22.2.0'
    

    It should be included within GradlePlease! And as an example to use it:

     <android.support.design.widget.TextInputLayout
        android:id="@+id/to_text_input_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <AutoCompleteTextView
            android:id="@+id/autoCompleteTextViewTo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="To"
            android:layout_marginTop="45dp"
            />
    </android.support.design.widget.TextInputLayout>
    

    Btw, the editor may not understand that AutoCompleteTextView is allowed within TextInputLayout.

    0 讨论(0)
  • 2020-12-02 08:59

    Use the TextInputLayout provided by the Material Components Library:

        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Label">
    
            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
        </com.google.android.material.textfield.TextInputLayout>
    

    0 讨论(0)
  • 2020-12-02 09:02

    You need to add the following to your module build.gradle file:

    implementation 'com.google.android.material:material:1.0.0'
    

    And use com.google.android.material.textfield.TextInputLayout in your XML:

           <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/text_input_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/my_hint">
    
            <EditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="UserName"/>
       </com.google.android.material.textfield.TextInputLayout>
    
    0 讨论(0)
  • 2020-12-02 09:02

    Import the Support Libraries, In your project's build.gradle file, add the following lines in the project's dependencies:

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

    Use following TextInputLayout in your UI Layout:

    <android.support.design.widget.TextInputLayout
        android:id="@+id/usernameWrapper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:hint="Username"/>
    
    </android.support.design.widget.TextInputLayout>
    

    Than, call setHint on TextInputLayout just after setContentView call because, to animate the floating label, you just need to set a hint, using the setHint method.

    final TextInputLayout usernameWrapper = (TextInputLayout) findViewById(R.id.usernameWrapper);
    usernameWrapper.setHint("Username");
    
    0 讨论(0)
提交回复
热议问题