我想创建一个文本输入:
1) 始终显示3行
2) 不允许用户输入超过 3 行可用空间的文本
3) 如果用户输入的文本超过 3 行,则不可滚动。
从技术上讲,我允许用户输入最多 500 个字符以保存到数据库,但我并不期望输入的文本量接近这个数量。因此,我对 maxLength 的使用只是预防性的。
这是我当前的 EditText:
<EditText android:id="@+id/edit_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="3"
android:maxLines="3"
android:maxLength="500"
android:inputType="textMultiLine" />
Problem
问题是,如果用户输入的文本超过 3 行,EditText 会添加额外的行并向下滚动到新行。它仅将用户限制为 500 个字符。
要控制引入的行,您可以添加一个 TextWatcher,以便每当用户引入新值时,您都可以处理错误(例如删除第三行之后的行并显示 toast)。您可以使用 \r 和 \n 检查新行。
http://developer.android.com/reference/android/text/TextWatcher.html http://developer.android.com/reference/android/text/TextWatcher.html
TextWatcher watcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
//here you can change edit text and show an error so the lines will never be 3
}
...}
EditText editText=findById(...);
editText.addTextChangedListener(watcher);;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)