nameInput.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.????_?????_??????) {
Integer inputLength2 = nameInput.getText().length();
String realTimeText = inputLength2.toString();
textView1.setText("Number of Characters: " + realTimeText);
}
return false;
}
});
nameInput是EditText类型的对象。我想在TextView上显示从EditText实时创建的String字符数。原理很简单,在我看来它会完美地工作(我需要做的就是“拦截”软键盘的字符,就像我在那里做的那样),但问题是:
http://developer.android.com/reference/android/view/inputmethod/EditorInfo.html
没有常数可用,这可能意味着我需要做一些不起眼的技巧来解决问题。你知道我怎么做吗?
答案 0 :(得分:3)
使用TextWatcher
:http://developer.android.com/reference/android/text/TextWatcher.html
您将收到有关输入更改的通知。例如:
private TextWatcher textWatcher = new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
Integer inputLength2 = s.length();
String realTimeText = inputLength2.toString();
textView1.setText("Number of Characters: " + realTimeText);
}
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
};
editText.addTextChangedListener(textWatcher);