Reputation: 4259
How do you prevent someone from entering the following into an EditText where setLines = 4?
dfa
d
ads
dd
adf
adf
ddf
ad
ddas
Upvotes: 0
Views: 486
Reputation: 7820
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...
Upvotes: 2