我知道您可以将EditText
的{{1}}设置为maxLines
。但是,如何实际限制EditText可以拥有的返回字符数(即当用户按下软键盘上的返回键时)?
我基本上不希望我的用户在我的EditText中执行此操作:
5
答案 0 :(得分:1)
您可以将TextWatcher
添加到EditText
并查看返回字符数量:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable s) {
int amountOfReturns = s.length() - s.toString().replace("\n", "").length();
//Here you can check amountOfReturns > MAX_RETURN_COUNT
//and do what you need, for example, prevent to change the text
}
});
答案 1 :(得分:0)
您可以使用以下代码:
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
editText.setText(s.toString().substring(5));
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});