我覺得這是更好的解決方案,因爲你只能在最後在你的EditText字段的任何地方按「確認」鍵,而不是線。
edittext.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int st, int ct,
int af) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() < 1 || start >= s.length() || start < 0)
return;
// If it was Enter
if (s.subSequence(start, start + 1).toString().equalsIgnoreCase("\n")) {
// Change text to show without '\n'
String s_text = start > 0 ? s.subSequence(0, start).toString() : "";
s_text += start < s.length() ? s.subSequence(start + 1, s.length()).toString() : "";
edittext.setText(s_text);
// Move cursor to the end of the line
edittext.setSelection(s_text.length());
}
}
});
'但是,這種方式似乎並沒有解決我的問題,因爲幾乎所有的關鍵將得到禁止,一旦我已經實施了監聽我的EditText.'..seams喜歡你的代碼是不正確的。它應該工作。與'文本守望者'你不能'輸入事件' –
爲什麼我的問題被拒絕投票? – mrkhv