2012-07-18 11 views
15

मेरे पास editText के लिए सत्यापन है। यदि editText फ़ील्ड खाली है तो उसे सत्यापन में विफल होना चाहिए और उपयोगकर्ता को Activity पर जाने के लिए रोकना चाहिए, क्योंकि एक मान आवश्यक है। यह कैसे करना है? मुझे पता है कि यह एक बुनियादी सवाल है, लेकिन मैं यह नहीं समझ सकता कि यह कैसे करें।संपादित करें टेक्स्ट फ़ील्ड किसी अन्य गतिविधि पर जाने से पहले आवश्यक है

मेरे कोड:

btninsert = (Button)findViewById(R.id.btn_insert); 
btninsert.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     insertValues(); 
     EditText userName = (EditText) findViewById(R.id.editText1); 

     if(userName.getText().toString().length() == 0) 
      userName.setError("First name is required!"); 

     Intent i = new Intent(getApplicationContext(), Login.class); 
     startActivity(i); 
    } 
}); 

जब यह Button उपयोगकर्ता क्लिक किया जाता है अगले स्क्रीन पर पुन: निर्देशित है, लेकिन मैं सत्यापन विफल हो जाता है कि अगर वे वर्तमान Activity पर ही रहती है और केवल जब मान्यता सफल हुआ की जरूरत है (यानी एक मान दर्ज किया गया है) वे अगले Activity पर जाते हैं।

उत्तर

39

यह आसान है ... जांचें कि आपका संपादन टेक्स्ट नीचे दिए गए उदाहरण में खाली है या नहीं।

if(TextUtils.isEmpty(userName.getText())){ 
    /** 
    * You can Toast a message here that the Username is Empty  
    **/ 

    userName.setError("First name is required!"); 

}else{ 
    Intent i = new Intent(getApplicationContext(), Login.class); 
    startActivity(i); 
} 
6

इस प्रयास करें:

bt.setOnClickListener(new OnClickListener() { 
    public void onClick(View arg0) 
    { 
     String str=et.getText().toString(); 

     if(str.equalsIgnoreCase("")) 
     { 
      et.setHint("please enter username");//it gives user to hint 
      et.setError("please enter username");//it gives user to info message //use any one // 
     } 
     else 
     { 
      Intent in=new Intent(getApplicationContext(),second.class); 
      startActivity(in); 
     } 
    } 
}); 
4

आप भी इस कोशिश कर सकते हैं:

if ((userName.getText().toString().trim().equals(""))) 
{ 
     Toast.makeText(getApplicationContext(), "User name is empty", Toast.LENGTH_SHORT).show(); 
} 
else 
{ 
     Intent i = new Intent(getApplicationContext(), Login.class); 
     startActivity(i); 
} 
1

बेहतर उपयोग करते हुए सभी आवश्यक या विशेष मान्यता के मामलों और setError के लिए शर्त ध्यान केंद्रित करने की है, तो खाली पाठ फ़ील्ड परीक्षण करने के लिए।

If(txtName.getText().toString().trim().equals("")) 
{ 
//Your message or any other validation 
} 
0

मुझे एक समान समस्या का सामना करना पड़ा। मेरे मामले में, मेरे पास सत्यापित करने के लिए बहुत सारे Edittexts थे, कुछ वे एक और लेआउट के बच्चे थे। युक्ति: सभी विचार एक ही रूट दृश्य में होना चाहिए। तो, सरल होने के लिए:

... 
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.viewGroup); 
checkFieldsRequired(viewGroup); 
... 

public boolean checkFieldsRequired(ViewGroup viewGroup){ 

    int count = viewGroup.getChildCount(); 
    for (int i = 0; i < count; i++) { 
     View view = viewGroup.getChildAt(i); 
     if (view instanceof ViewGroup) 
      checkFieldsRequired((ViewGroup) view); 
     else if (view instanceof EditText) { 
      EditText edittext = (EditText) view; 
      if (edittext.getText().toString().trim().equals("")) {     
       edittext.setError("Required!");     
      } 
     } 
    } 

    return false; 
} 
1

मैं समझता हूँ कि यह एक पुराने सवाल है लेकिन, आप .equals()

if(userName.getText().toString().isEmpty()){ 

    /** 
    * You can Toast a message here that the Username is Empty  
    **/ 

    userName.setError("First name is required!"); 

}else{ 
    Intent i = new Intent(getApplicationContext(), Login.class); 
    startActivity(i); 
} 
की .isEmpty (उपयोग कर सकते हैं हो सकता है इस में मदद करता है) के बजाय

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

  • कोई संबंधित समस्या नहीं^_^