2015-06-01 8 views
39

Vor kurzem Google eingeführt neue Android Design-Bibliothek in das TextInputLayout Feld verwenden, um die Floating Hint-Funktion von EditText zu aktivieren.So verwenden Sie TextInputLayout in der neuen Android-Design-Bibliothek

Nicht viel Anleitung ist verfügbar here.

Diese Seite sagt

Sie jetzt in einem TextInputLayout wickeln kann

aber keine Ahnung, weil intelligente Vorhersage (Strg + SPACE) wird prognostiziert keine Attribute zum TextInputLayout. Meine Fragen sind also:

Wie erhalten wir den EditText, der dieser Komponente zugrunde liegt?

Wie können wir Daten von EditText erhalten?

+0

Die Frage ist klar und nützlich ... – dit

+0

Diese Frage ist klar und ziemlich nützlich, da es nicht viel Anleitung für die Implementierungen der Design Library gibt! – sud007

+1

Check ** [dieses Tutorial] (http://chintanrathod.com/textinputlayout-material-design-support-library-tutorial/) **, es ist hilfreich. –

Antwort

49

TextInputLayout erweitert ViewGroup Klasse. Also was bedeutet, dass Sie Ihre EditText in eine TextInputLayout wickeln müssen.

<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="hint" 
     android:id="@+id/editText1" /> 

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

Es funktioniert nur auf Android 5.0 und höher, oder? – moictab

+7

@moictab Es befindet sich in der Design-Support-Bibliothek und ist daher für API 7+ verfügbar. –

+1

Nun, ich habe es in API 19 versucht und ich habe eine Ausnahme beim Aufblasen der Ansicht. Vielleicht habe ich etwas falsch gemacht. – moictab

4

Dies ist in design support library unter "Floating-Etikett für die Bearbeitung von Text" definiert.

 <android.support.design.widget.TextInputLayout 
      android:id="@+id/name_et_textinputlayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="@dimen/activity_vertical_margin"> 

      <EditText 
       android:id="@+id/FeedBackerNameET" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:hint="hinttext" 
       android:inputType="textPersonName|textCapWords" /> 
    </android.support.design.widget.TextInputLayout> 
1

Damit dies funktioniert gut, sollten Sie Sie app Thema aus Theme.AppCompat erweitert lassen (oder deren Nachkommen) Thema, von Theme.AppCompat.Light.NoActionBar erstreckt wie.

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

    <EditText 
     android:id="@+id/et_message" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/type_message" 
     android:maxLines="5"/> 

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

Hinweis wird automatisch verschieben, sobald Sie mit der Eingabe in dem Bearbeiten von Text beginnen und diese Fehler zu haben, die unter Bearbeiten von Textsatz Fehlern auf Texteingabe-Layout wie diese erscheint nicht auf Text bearbeiten

tilMessage.setError("Message field is empty!"); 

zu deaktivieren Fehler

tilMessage.setErrorEnabled(false); 
3

wickeln Sie das TextInputLayout um TextInputEditText statt EditText.

Wrapping um EditText hat Problem im Querformat-Modus. Weitere Informationen finden Sie unter this article.

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

    <android.support.design.widget.TextInputEditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="hint" 
     android:id="@+id/editText1" /> 

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

Wir können AppCompactEditText verwenden auch für diesen Bedarf Unterstützung hinzuzufügen: appcompat in gradle

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

    <android.support.v7.widget.AppCompatEditText 
     android:id="@+id/et_name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/hint" 
     android:maxLength="100" 
     android:inputType="textNoSuggestions" 
     android:textColor="@color/colorPrimary" 
     android:textSize="@dimen/font_size_large" /> 

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

der richtige Weg ...

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

      <android.support.design.widget.TextInputEditText 
       android:id="@+id/card_id_edit_text" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:hint="@string/form_card_id_text" 
       android:inputType="number" 
       android:maxLength="10"/> 
     </android.support.design.widget.TextInputLayout> 

Wenn Sie EditText wie ein Kind verwenden von TextInputLayout wird diese Nachricht in Android Monitor angezeigt:

I/TextInputLayout: EditText hinzugefügt ist kein TextInputEditText. Bitte wechseln Sie stattdessen zur Verwendung dieser Klasse.

Verwandte Themen