जवाबों:
Google के TextInputLayoutभाग के रूप में पेश किए जाने के बाद से तृतीय-पक्ष लाइब्रेरी का उपयोग करने की कोई आवश्यकता नहीं है design-support-library।
एक बुनियादी उदाहरण के बाद:
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name" />
</android.support.design.widget.TextInputLayout>
नोट: इसकीapp:errorEnabled="true" विशेषता के रूप में सेट करने से TextInputLayoutयह किसी त्रुटि के प्रदर्शित होने के बाद आकार में परिवर्तित नहीं होगा - इसलिए यह मूल रूप से अंतरिक्ष को अवरुद्ध करता है।
नीचे दी गई त्रुटि को दिखाने के लिए EditTextआपको बस (बच्चे पर नहीं ) कॉल #setErrorकरना होगा :TextInputLayoutEditText
TextInputLayout til = (TextInputLayout) findViewById(R.id.text_input_layout);
til.setError("You need to enter a name");

त्रुटि को छिपाने के लिए और टिंट को बस कॉल करने के लिए रीसेट करें til.setError(null)।
उपयोग करने के लिए TextInputLayoutआपको अपनी build.gradleनिर्भरता में निम्नलिखित को जोड़ना होगा :
dependencies {
compile 'com.android.support:design:25.1.0'
}
डिफ़ॉल्ट रूप से EditTextवसीयत की रेखा लाल होगी। यदि आपको एक अलग रंग प्रदर्शित करने की आवश्यकता है तो आप कॉल करते ही निम्न कोड का उपयोग कर सकते हैं setError।
editText.getBackground().setColorFilter(getResources().getColor(R.color.red_500_primary), PorterDuff.Mode.SRC_ATOP);
इसे साफ़ करने के लिए बस clearColorFilterफ़ंक्शन को इस तरह से कॉल करें:
editText.getBackground().clearColorFilter();
textInputLayout.setError("Error messsage"), रंग EditTextलाल होना चाहिए। इसे रीसेट करने के लिए, यह कॉल करने के लिए पर्याप्त है textInputLayout.setError(null)।
editText.getBackground().setColorFilter(getResources().getColor(R.color.red_500_primary), PorterDuff.Mode.SRC_ATOP);अब नवीनतम समर्थन पुस्तकालय के साथ की जरूरत नहीं है
EditText, पर नहीं TextInputLayout। मैंने इस उत्तर को देखा और अभी भी यह पता नहीं लगा सका कि मुझे क्या बदलना है। बहुत आसान बात याद आती है।
अपने EditTextआप को एक में लपेटा जाना चाहिएTextInputLayout
<android.support.design.widget.TextInputLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tilEmail">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/etEmail"
android:hint="Email"
android:layout_marginTop="10dp"
/>
</android.support.design.widget.TextInputLayout>
जैसा आप चाहते थे एक त्रुटि संदेश प्राप्त करने के लिए, त्रुटि सेट करें TextInputLayout
TextInputLayout tilEmail = (TextInputLayout) findViewById(R.id.tilEmail);
if (error){
tilEmail.setError("Invalid email id");
}
आपको डिज़ाइन समर्थन लाइब्रेरी निर्भरता जोड़ना चाहिए। इस रेखा को अपनी श्रेणी निर्भरता में जोड़ें
compile 'com.android.support:design:22.2.0'
reVerse का जवाब बहुत अच्छा है लेकिन इसने यह नहीं बताया कि फ्लोटिंग एरर टूलटिप किस तरह का है
आपको इसे edittext.setError(null)हटाने की आवश्यकता होगी ।
इसके अलावा, जैसा कि किसी ने बताया, आपको ज़रूरत नहीं हैTextInputLayout.setErrorEnabled(true)
ख़ाका
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter something" />
</android.support.design.widget.TextInputLayout>
कोड
TextInputLayout til = (TextInputLayout) editText.getParent();
til.setError("Your input is not valid...");
editText.setError(null);
TextInputLayout til = (TextInputLayout)editText.getParent();
til.setErrorEnabled(true);
til.setError("some error..");
private EditText edt_firstName;
private String firstName;
edt_firstName = findViewById(R.id.edt_firstName);
private void validateData() {
firstName = edt_firstName.getText().toString().trim();
if (!firstName.isEmpty(){
//here api call for ....
}else{
if (firstName.isEmpty()) {
edt_firstName.setError("Please Enter First Name");
edt_firstName.requestFocus();
}
}
}
EditText। अधिक संभावना है, आपको कुछ ऐसी चीज की आवश्यकता होगी जो लपेटती हैEditTextया अन्यथा इसमें जुड़ जाती है। Github.com/rengwuxian/MaterialEditText देखें ।