我试图通过编程方式将编辑文本的最大行更改为像3这样的数字,我已经尝试了这行代码,但它没有正常工作:
binding.editText.maxLines = 3
属性maxLines对应于EditText的最大高度,它控制外部边界而不是内部文本行。如果您想更改行数,请使用以下代码:
// set listeners
txtSpecialRequests.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
lastSpecialRequestsCursorPosition = txtSpecialRequests.getSelectionStart();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
txtSpecialRequests.removeTextChangedListener(this);
if (txtSpecialRequests.getLineCount() > 3) {
txtSpecialRequests.setText(specialRequests);
txtSpecialRequests.setSelection(lastSpecialRequestsCursorPosition);
}
else
specialRequests = txtSpecialRequests.getText().toString();
txtSpecialRequests.addTextChangedListener(this);
}
});
你可以在txtSpecialRequests.getLineCount()>3 .