2017-05-24 7 views

उत्तर

0

आप नीचे के रूप में लेआउट xml फ़ाइल नीचे का उपयोग कर सकते हैं।

<?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> 

सूचना है कि android:hint="Placeholder"TextInputEditText से TextInputLayout से android:hint="Label" साथ एक ही समय में दिखाई दे रहा है जब दृश्य ध्यान केंद्रित नहीं है। आप उस लेबल को दिखाने और छिपाने के लिए अपने जावा कोड में कुछ अतिरिक्त जांच कर सकते हैं। या TextInputLayout से android:hint="Placeholder" छोड़ दें।

रंग बदलने के लिए, आपको android:theme="@style/TextLabel का उपयोग करके TextInputLayout का उपयोग करके थीम सेट करने की आवश्यकता है और वहां आपका रंग उच्चारण सेट किया गया है।

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

यदि आप टेक्स्टिनपुटलेआउट का उपयोग करते हैं तो संपादन के फ़ोकस पर, आपको कोई प्लेसहोल्डर नहीं मिला।

लेआउट:

<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> 

आप edittext का ध्यान देने के परिवर्तन श्रोता सेट करना होगा।

जावा:

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

आप इस TextInputLayout और EditText उपयोग कर सकते हैं।

<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. गुण जोड़ें android:hint="Label"TextInputLayout को हमेशा अपने संकेत Label को दिखाने के लिए:

यहाँ अपने एक्सएमएल है।

2. प्रोग्राम EditText संकेत Placeholder केवल जब EditText ध्यान केंद्रित करने के लिए सेट करें।

अपनी गतिविधि में लाइनों के नीचे जोड़ें:

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

    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(""); 
      } 
     } 
    }); 

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

उत्पादन:

enter image description here

आशा इस में मदद मिलेगी ~

+0

वहाँ लेबल रखने के लिए और विकेन्द्रित राज्य में स्क्रीनशॉट तरह प्लेसहोल्डर के लिए एक रास्ता है? –

+0

हां यह संभव है। 'एंड्रॉइड का उपयोग करें: फोकस करने योग्यInTouchMode =" true "' पैरेंट/कंटेनर लेआउट के लिए। – FAT

2

आप कोड निम्नलिखित (kotlin में उपयोग कर सकते हैं)। यह 200 एमएस देरी के बाद प्लेसहोल्डर दिखाएगा (संकेत और प्लेसहोल्डर ओवरलैपिंग से बचने के लिए)।

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 = "" 
      } 
     } 
    } 
} 

और फिर लेआउट एक्सएमएल कक्षा में:

<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> 
संबंधित मुद्दे