2012-10-04 9 views
0

J'ai un edittext, je veux juste montrer un message quand un caractère ou un mot anglais est inséré via le clavier, que dois-je faire?droid: Comment puis-je valider l'entrée EditText?

+0

utilisation TextWatcher (http://developer.android.com/reference/android/text/TextWatcher.html) et la Validation (http://blog.donnfelker.com/2011/11/23/android-validation-with-edittext/). –

+0

http://stackoverflow.com/questions/7432083/how-to-use-edittext-ontextchanged-event-when-i-press-the-number – Bush

Répondre

0

Il suffit de faire comme ça -

((EditText)findViewById(R.id.et_testo)).addTextChangedListener(new TextWatcher() { 

public void afterTextChanged(Editable s) { 

} 

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

} 

public void onTextChanged(CharSequence s, int start, int before, 
     int count) { 
    // TODO Auto-generated method stub 
    Toast.makeToast(activity.this, s.toString(), Toast.Long).show(); 
} 
}); 

Jetez un oeil à TextWatcher

0

Utilisez TextWatcher pour regarder le texte dans le EditText et chaque fois qu'un caractère anglais ou le mot est détecté dans « OnTextChanged » mehod de TextWatcher vous pouvez afficher un message.

Un exemple est le suivant

TextWatcher textWatcher = new TextWatcher() { 

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

      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
       // or here 
      } 

      public void afterTextChanged(Editable s) { 
       // Check for English Character or word here and show message 
     }; 

     // Set listener on the original text field 
     itemText.addTextChangedListener(textWatcher); 

    } 

Hope it helps !!!

+0

merci, votre code a été utile. – anonymous

+0

@malina Puis upvote et accepte la réponse :) –

Questions connexes