2013-08-14 5 views
20

की नोक पर त्रुटि दिखाएं यदि उपयोगकर्ता edittext में रिक्त मान दर्ज करता है तो मैं त्रुटि दिखाना चाहता हूं। लेकिन मुझे यह नहीं मिल रहा है कि मैं यह कैसे कर सकता हूं। इस तरह मैं इसे पसंद करना चाहता हूं:संपादन टेक्स्ट एंड्रॉइड

enter image description here

यह मेरा एक्सएमएल है कि मैं बनाया है है

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:id="@+id/headerLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/top_bg" 
     android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/back_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="5dp" 
      android:src="@drawable/back_button" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="75dp" 
      android:layout_marginTop="10dp" 
      android:text="Traveller Details" 
      android:textColor="@android:color/white" /> 
    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/tittleLayout" 
     android:layout_below="@+id/headerLayout" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/TittleTravellerDetails" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="5dp" 
      android:gravity="left" 
      android:text="Traveller Details" /> 

     <View 
      android:layout_width="wrap_content" 
      android:layout_height="2dip" 
      android:layout_marginTop="2dp" 
      android:background="#FF909090" /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/passengerDetails" 
     android:layout_below="@+id/tittleLayout" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:orientation="vertical"> 

     <Spinner 
      android:id="@+id/Tittle" 
      android:layout_width="290dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:layout_height="wrap_content"/> 
     <EditText 
      android:id="@+id/firstName" 
      android:layout_width="290dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:hint="First Name" /> 

     <EditText 
      android:id="@+id/LastName" 
      android:layout_width="290dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:hint="Last Name" /> 

    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/ContactDetailsLayout" 
     android:layout_below="@+id/passengerDetails" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_marginTop="10dp" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/TittleContactDetails" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="5dp" 
      android:gravity="left" 
      android:text="ContactDetails" /> 

     <View 
      android:layout_width="wrap_content" 
      android:layout_height="2dip" 
      android:layout_marginTop="2dp" 
      android:background="#FF909090" /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/mobileEmailDetails" 
     android:layout_below="@+id/ContactDetailsLayout" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:orientation="vertical"> 

     <EditText 
      android:id="@+id/mobileNumber" 
      android:layout_width="290dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:inputType="number" 
      android:hint="Mobile No" /> 

     <TextView 
      android:id="@+id/emailid" 
      android:layout_width="284dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="8dp" 
      android:layout_marginTop="5dp" 
      android:hint="Email ID" /> 

    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/continueBooking" 
     android:layout_below="@+id/mobileEmailDetails" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:orientation="vertical"> 

     <ImageView 
      android:id="@+id/continuebooking" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="20dp" 
      android:layout_marginTop="25dp" 

      android:src="@drawable/continue" /> 

    </LinearLayout> 

</RelativeLayout> 

गतिविधि कोड

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 
     emailId = (TextView)findViewById(R.id.emailid); 
     continuebooking = (ImageView)findViewById(R.id.continuebooking); 
     firstName= (EditText)findViewById(R.id.firstName); 
     emailId.setText("[email protected]"); 
     setTittle(); 
     continuebooking.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       if(firstName.getText().toString().trim().equalsIgnoreCase("")){ 
        firstName.setError("Enter FirstName"); 
       } 

      } 
     }); 
    } 

तो, अगर उपयोगकर्ता अपने पहले नाम दर्ज नहीं करते मैं छवि है कि यू that.Please की तरह किसी भी नाम कुछ दर्ज नहीं किया है की तरह एक त्रुटि दिखाने के लिए मेरी मदद करो मैं android.Thanks

में नया हूँ
+0

तो तुम बुलबुले के नुकीले अंत बाईं ओर कुछ पिक्सल ले जाना चाहते है? – ZooMagic

उत्तर

1

के साथ चाहते हैं youredittext.equals("") आप जान सकते हैं कि उपयोगकर्ता ने कोई पत्र दर्ज नहीं किया है।

+0

मुझे पता है कि मैं संपादन टेक्स्ट की नोक पर उस त्रुटि को दिखाना चाहता हूं। कृपया संदर्भ – Developer

68

आप EditText

if (editText.getText().toString().trim().equalsIgnoreCase("")) { 
     editText.setError("This field can not be blank"); 
} 

की पॉपअप से त्रुटि दिखा सकते हैं और उस के रूप में

enter image description here

firstName.addTextChangedListener(new TextWatcher() { 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     if (firstName.getText().toString().length <= 0) { 
      firstName.setError("Enter FirstName"); 
     } else { 
      firstName.setError(null); 
     } 
    } 
}); 
+0

के लिए छवि देखें, मुझे पता है कि मैं संपादन टेक्स्ट की नोक पर उस त्रुटि को दिखाना चाहता हूं। कृपया संदर्भ के लिए छवि देखें – Developer

+0

@ गौरव मैंने वही चीज़ दी जो आप चाहते हैं। इस – SilentKiller

+0

को आजमाएं, लेकिन फिर यदि मैं कुछ मूल्य दर्ज कर रहा हूं तो यह त्रुटि – Developer

0

आप एक मान्यता को गति प्रदान करने के लिए एक onchange घटना इस्तेमाल कर सकते हैं इस प्रकार एक की तरह लग रहे हो जाएगा और फिर आप एक टोस्ट प्रदर्शित कर सकते हैं यदि यह आपके लिए पर्याप्त है

18
private void showError() { 
    mEditText.setError("Password and username didn't match"); 
} 

कौन इस तरह दिखाया त्रुटियों में परिणाम होगा:

enter image description here

और आप इसे निकालना चाहते हैं:

textView.setError(null); 
+1

बदल गया है कि हम इस त्रुटि को कैसे हटा सकते हैं – Developer

+1

textView.setError (शून्य); –

0

आप onClick घटना में अपने कोड लिखा है। जब आप EditText पर क्लिक करेंगे तो यह कॉल करेगा। लेकिन ऐसा कुछ ऐसा है जैसे आप प्रवेश करने से पहले इसे जांच रहे हैं।

तो मेरा सुझाव क्या है, आपको focus changed का उपयोग करना चाहिए। जब कोई दृश्य फोकस करता है, तो आप no error सेट कर रहे हैं और जब फ़ोकस बदल जाता है, तो आप जांचते हैं कि वैध इनपुट है या नहीं।

firstName.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View arg0, boolean arg1) { 
     firstName.setError(null); 
     if (firstName.getText().toString().trim().equalsIgnoreCase("")) { 
      firstName.setError("Enter FirstName"); 
     } 
    } 
}); 
+0

मुझे नहीं मिल रहा है कि मैं इसका उपयोग कैसे करूं ?? – Developer

+1

@ गौरव, मैंने इस परियोजना को भी लागू नहीं किया। रुको .. जल्द ही जांच लेंगे .. –

+0

@ गौरव, क्या आप मुझे बता सकते हैं कि आप किस सटीक समस्या का सामना कर रहे हैं? अन्यथा पहले अप-वोट किए गए उत्तर ऐसा करने के लिए पर्याप्त हैं .. –

3

मैं मेरी समस्या आशा की समाधान मिल गया इस में मदद मिलेगी दूसरों also.I का इस्तेमाल किया है onTextChnaged यह आह्वान एक प्रकार का ऑब्जेक्ट एक संपादन योग्य से जुड़ा हुआ है, तब उसके तरीकों जब पाठ बदल गया है बुलाया जाएगा।

public class MainActivity extends Activity { 
    TextView emailId; 
    ImageView continuebooking; 
    EditText firstName; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 
     emailId = (TextView)findViewById(R.id.emailid); 
     continuebooking = (ImageView)findViewById(R.id.continuebooking); 
     firstName= (EditText)findViewById(R.id.firstName); 
     emailId.setText("[email protected]"); 
     setTittle(); 
     continuebooking.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       if(firstName.getText().toString().trim().equalsIgnoreCase("")){ 
        firstName.setError("Enter FirstName"); 
       } 

      } 
     }); 
     firstName.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       firstName.setError(null); 

      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       firstName.setError(null); 

      } 
     }); 
    } 
0

ऐसा लगता है कि आप संपादन टेक्स्ट के अंत में त्रुटि दिखाने के लिए नहीं मिल सकते हैं। पैरेंट लेआउट लिफाफे के मिलान के लिए अपनी संपादन टेक्स्ट चौड़ाई सेट करें। ठीक काम करेगा।

2
if(TextUtils.isEmpty(firstName.getText().toString()){ 
     firstName.setError("TEXT ERROR HERE"); 
} 

या आप भी TextInputLayout उपयोग कर सकते हैं जो कुछ उपयोगी विधि और कुछ उपयोगकर्ता के अनुकूल एनीमेशन

संबंधित मुद्दे