行末だけでなく、EditTextフィールドの任意の場所で「Enter」を押すことができるため、これがより良い解決策だと思います。
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());
}
}
});