How do you prevent someone from entering the following into an EditText where setLines = 4?
dfa
d
ads
dd
adf
adf
ddf
ad
ddas
Well, you can't really prevent someone from entering more than four lines. If he just types without manually adding newlines, you will never know how many lines he entered.
However, you can limit the number of manually added newlines (via the enter key) by hooking on the OnKeyListener
of the EditView
:
EditText edit = (EditText)findViewById(R.id.edit);
edit.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
int lineCount = ((EditText)v).getText().toString().split("\\n").length;
if (lineCount > 3) {
return true;
}
}
return false;
}
});
It's quite a hacky method, but the only I can come up with so far...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.