我想限制每行字符为15个字符。在每隔15个字符之后,我应该在该行中包含/添加一个“输入”键(“\ n”),以便用户可以不停止写入。任何建议?如何限制多行编辑文本中每行的字符?
这是我EditText
XML
<EditText
android:id="@+id/ET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:maxLength="20"
android:inputType="textMultiLine"
android:visibility="gone" />
这是我的ET TextChangedListener
ET.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (count % 20 == 0)
{
((Editable) s).append("\n");
ET.setSelection(ET.getText().length());
}
}
});
为什么不以编程方式编写此代码?当它达到15个字符时,则换行 – lantonis
@lantonis,因为它不会改变。它包含“\ n”,但即使试图将光标移动到末尾时,它仍停留在该行的末尾 –
http://stackoverflow.com/questions/10933412/how-to-set-maximum -characters-per-line-for-text-view-in-android –