2012-10-04 44 views
0

我有一個編輯文本,我只是想通過鍵盤插入英文字符或單詞時顯示一條消息,我該怎麼辦?droid:我如何驗證EditText輸入?

+0

使用TextWatcher(http://developer.android.com/reference/android/text/TextWatcher.html)和驗證都(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

回答

0

只是做這樣的 -

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

看一看TextWatcher

0

使用TextWatcher看在的EditText文字和無論何時檢測到英文字母或者單詞「onTextChanged」 TextWatcher的mehod可以顯示一個消息。

一個例子是如下

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

    } 

希望它能幫助!

+0

謝謝,你的代碼很有幫助。 – anonymous

+0

@malina然後upvote並接受答案:) –