我有一个带有侦听器的 editText。
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event)
{
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
return true;
}
return false;
}
});
现在我想在我的程序中使用用户在文本框中输入的数据,而无需用户按 Enter 键。 (如果我删除(keyCode == KeyEvent.KEYCODE_ENTER)
然后程序由于某种原因挂起)。
我应该怎么做才能识别用户在文本框中输入了某些字符?
我认为你想要实现这样的功能:每当用户输入你想要检查的任何字符时,如果是这种情况,那么就会有一个文本观察器 http://developer.android.com/reference/android/text/TextWatcher.html为你上课。您可以在其中重写它的 3 个方法:afterTextChanged
, beforeTextChanged
and onTextChanged
.
mPasswordLength = (EditText)findViewById(R.id.password_length);
mPasswordLength.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)