gpt4 book ai didi

具有固定最大行数且无滚动的Android EditText

转载 作者:太空狗 更新时间:2023-10-29 16:38:16 29 4
gpt4 key购买 nike

我想创建一个文本输入:

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" />

问题

问题在于,如果用户输入的文本多于 3 行,EditText 会添加额外的行并向下滚动到新行。它仅将用户限制为 500 个字符。

最佳答案

要控制引入的行,您可以添加一个 TextWatcher,这样每当用户引入一个新值时,您就可以处理错误(例如,删除第三行之后的行并显示 toast )。您可以使用\r 和\n 检查新行。

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);;

关于具有固定最大行数且无滚动的Android EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22869687/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com