2015-11-05 2 views
1

Voici le problème avec double soupçon - TextInputLayout and EditText double hint issuebesoin de faire à double pointe en EditText avec TextInputLayout

je dois faire la même chose. J'ai besoin de deux indices. L'un est un titre de EditText, et le second est une valeur possible si rien n'a été entré.

Mais malheureusement, il n'y a qu'un seul tag:

android:hint 

Puis-je faire quelque chose avec elle?

UPD1:

La réponse était de mettre en indice pour EditText et programatically allusion pour TextInputLayout en XML.

 <android.support.design.widget.TextInputLayout 
      android:layout_width="match_parent" 
      android:hint="@string/prompt_quantity" 
      android:layout_height="wrap_content"> 

      <EditText 
       android:id="@+id/quantity" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:inputType="number" 
       android:maxLines="1" 
       android:singleLine="true" /> 

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

Et

quantityEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      quantityEditText.setHint(hasFocus ? "0" : ""); 
     } 
    }); 
+0

Utilisez cette LIB. https://github.com/activesince93/CustomViews –

Répondre

0

Il suffit de vérifier votre entrée et changer l'indice en fonction de ce que:

EditText editText = (EditText)findViewById(R.id.editText); 
editText.addTextChangedListener(new TextWatcher() 
{ 
    @Override 
    public void afterTextChanged(Editable s) 
    { 
     if(editText.getText().toString().equals("")) 
      editText.setHint(""); 
     else 
      editText.setHint("Your normal hint"); 
    } 
}); 
0

Essayez cette façon

xml layout

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_gravity="center_vertical" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 
    <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/non_active_edit_text" 
      android:focusable="false" 
      android:gravity="right|center_vertical" 
      android:hint="Right" 
      android:paddingTop="4dp" 
      android:background="@null"/> 


    <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/active_edit_text" 
      android:hint="Left" 
      android:layout_gravity="center_horizontal"/> 
</merge> 

JAVA classe

public class TwoHints extends RelativeLayout{ 

    EditText activeField; 
    EditText nonActiveEditText; 

    final CharSequence hint; 
    public TwoHints(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     LayoutInflater.from(context).inflate(R.layout.two_hints_edit_text, this); 
     activeField = (EditText)findViewById(R.id.active_edit_text); 
     nonActiveEditText = (EditText)findViewById(R.id.non_active_edit_text); 
     hint = nonActiveEditText.getHint(); 
     activeField.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       nonActiveEditText.setHint(s.length() !=0 ? "" : hint); 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 
    } 
} 

editetext pour xml

<you.package.TwoHints 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"/>