Tutorials  /  JavaScript

Mastering Material Design Text Fields in Android Apps

Ccentron Redaktion · September 2024 ·3 Min. Lesezeit ·JavaScript, Tutorial

Entdecken Sie die Welt der Android-Material-Textfelder in unserem neuesten Blogbeitrag! Tauchen Sie ein in die Vielfalt der Designoptionen für Textfelder in Ihren Apps und lernen Sie, wie Sie mit der Material Design Components-Bibliothek ein beeindruckendes Benutzererlebnis schaffen können. Von Standard- und dichten Textfeldern bis hin zu geformten und konturierten Varianten - optimieren Sie Ihre Benutzeroberfläche mit unseren Expertentipps!

Material Textfelder und TextInputLayout

Die TextInputLayout-Klasse bietet eine Implementierung für Material-Textfelder. Dafür verwenden wir einfach das TextInputEditText. Zuerst müssen wir die neue Abhängigkeit der Materialkomponenten importieren und das MaterialComponent-Theme in unserer Aktivität festlegen.

Code
implementation 'com.google.android.material:material:1.1.0-alpha09'

Ein Eingabetextfeld hat standardmäßig einen gefüllten Hintergrund, um die Aufmerksamkeit des Benutzers zu erregen. Ein einfaches Textfeld kann wie folgt erstellt werden:

Code
<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:hint="Gefülltes Feld (Standard)">

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

</com.google.android.material.textfield.TextInputLayout>
VM

Passende Infrastruktur bei centron

Node-Anwendungen wollen deployt werden: ccloud³ VMs mit vollem Root-Zugriff ab 3,12 € im Monat – oder gleich als Managed Server, wenn der Betrieb nicht bei Ihnen liegen soll. Cloud Server mieten →

Standard- und dichte Textfelder

Textfelder können zwei Höhenvarianten haben: Standard und dicht. Das dichte Textfeld ist etwas kürzer in der Höhe.

Code
<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:hint="Gefülltes Feld (Standard)">

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

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
   
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:hint="Gefülltes Feld (Dicht)"
    app:boxBackgroundColor="#20D81B60">

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

</com.google.android.material.textfield.TextInputLayout>

Umrandete Textfelder

Um das umrandete Aussehen für Textfelder zu erreichen, wenden Sie den folgenden Stil auf das TextInputLayout an:

Code

Ähnlich wie bei gefüllten Feldern gibt es hier zwei Höhenvarianten: Standard und dicht.

Ende-Icon-Modi

Ende-Icons sind Symbole, die rechts vom Textfeld platziert werden können. Es gibt drei Arten von eingebauten Icons: password_toggle, clear_text und benutzerdefiniert.

Code
<com.google.android.material.textfield.TextInputLayout
   
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:hint="Passwort eingeben"
    app:endIconMode="password_toggle">

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

</com.google.android.material.textfield.TextInputLayout>

Geformte Textfelder

Mit ShapeAppearance können wir die Form des Textfelds anpassen. Es gibt zwei eingebaute Formen: 'cut' und 'round'.

Code
<style name="Cut" parent="ShapeAppearance.MaterialComponents.MediumComponent">
    <item name="cornerFamily">cut</item>
    <item name="cornerSize">12dp</item>
</style>

<style name="Rounded" parent="ShapeAppearance.MaterialComponents.SmallComponent">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">16dp</item>
</style>

Fazit

Die Material Design Components-Bibliothek bietet eine Vielzahl von Optionen zur Anpassung von Textfeldern in Android-Apps. Von gefüllten und umrandeten Feldern bis hin zu benutzerdefinierten End-Icons und geformten Textfeldern haben Entwickler zahlreiche Möglichkeiten, eine ansprechende Benutzeroberfläche zu gestalten.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren