2017-05-24 1 views

Antwort

0

Sie können wie unten unten Layout XML-Datei verwenden.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<TextView 
    android:text="Label" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView2" 
    android:textColor="@color/wallet_holo_blue_light" /> 
<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="textPersonName" 
    android:text="Name" 
    android:ems="10" 
    android:id="@+id/editText2" 
    android:hint="Placeholder" /> 
    </LinearLayout> 
2
<android.support.design.widget.TextInputLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Label"> 

    <android.support.design.widget.TextInputEditText 
     android:hint="Placeholder" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textEmailAddress" /> 

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

Beachten Sie, dass android:hint="Placeholder" von TextInputEditText zugleich mit android:hint="Label" von TextInputLayout sichtbar ist, wenn Ansicht fokussiert nicht. Sie könnten Ihren Java-Code zusätzlich überprüfen, um das Label anzuzeigen und auszublenden. Oder lassen Sie einfach android:hint="Placeholder" von TextInputLayout.

Um die Farbe zu ändern, müssen Sie ein Thema mit android:theme="@style/TextLabel für TextInputLayout setzen und dort Ihren Farbakzent einstellen.

<style name="TextLabel" parent="TextAppearance.AppCompat.Light"> 
    <item name="colorAccent">@color/yourColor</item> 
</style> 
0

Wenn Sie textinputlayout dann auf Fokus von edittext verwenden, haben Sie keinen Platzhalter erhalten.

Layout:

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

    <EditText 
     android:id="@+id/username_txt" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

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

Sie haben einen Fokuswechsel Hörer des EditText einzustellen.

Java:

usernameTxt.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     if (hasFocus) { 
      usernameTxt.setHint("Label"); 
     } else { 
      usernameTxt.setHint("Placeholder"); 
     } 
    } 
}); 
6

Sie können diese TextInputLayout und EditText mit tun.

Hier ist Ihre XML:

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

    <EditText 
     android:id="@+id/edit_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="text" /> 

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

1. Attribut hinzufügen android:hint="Label" zu TextInputLayoutLabel immer seine Hinweise zu zeigen.

2. gesetzt Programmatically EditText Hinweise Placeholder nur, wenn EditText konzentriert erhalten.

unten Zeilen in Ihrer Aktivität hinzufügen:

......... 
    ................. 

    final EditText editText = (EditText) findViewById(R.id.edit_text); 

    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View view, boolean hasFocus) { 
      if (hasFocus) { 
       editText.setHint("Placeholder"); 
      } else { 
       editText.setHint(""); 
      } 
     } 
    }); 

    ......... 
    .................. 

OUTPUT:

enter image description here

Hope this ~

+0

Gibt es eine Möglichkeit, das Etikett zu halten und wie der Screenshot in unfokussiert Zustand Platzhalter? –

+0

Ja ist es möglich. Verwenden Sie 'android: focusableInTouchMode =" true "' zum Eltern/Container-Layout. – FAT

2

helfen Sie folgenden Code (in Kotlin verwenden können). Es wird Platzhalter nach 200 ms Verzögerung angezeigt (um überlappende Hinweise und Platzhalter zu vermeiden).

class PlaceholderEditText : TextInputEditText { 

    constructor(context: Context) : super(context) 
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) 
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) 

    private val placeholder = hint 

    init { 
     hint = "" 
     onFocusChangeListener = OnFocusChangeListener { _, hasFocus -> 
      if (hasFocus) { 
       postDelayed({ hint = placeholder }, 200) 
      } else { 
       hint = "" 
      } 
     } 
    } 
} 

und dann in der Klasse Layout xml:

<android.support.design.widget.TextInputLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="ALWAYS VISIBLE LABEL"> 

     <com.myapp.widget.PlaceholderEditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="DISAPPEARING PLACEHOLDER" /> 

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