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

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

本文介绍了具有最大行数固定且不滚动的Android EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个文本输入,

I would like to create a text input that:

1)始终显示3行

2)不允许用户输入超过3行中可用空间的文本

2) Does not allow the user to enter any more text than the available space in the 3 lines

3)如果用户输入的文本多于3行,则不可滚动.

3) Is not scrollable, if users enters text greater than 3 lines.

从技术上讲,我允许用户输入最多500个字符以保存到数据库,但我不希望输入的文本量接近此数量.因此,我对maxLength的使用只是出于预防目的.

Technically, I allow for the user to enter up to 500 chars for saving to DB, but I am not expecting near this amount of text for the input. So, my usage of maxLength is only precautionary.

这是我当前的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个字符.

The problem is that if the user enters text greater than 3 lines, the EditText adds additional lines and scrolls downward into the new line. It only limits the user to 500 characters.

推荐答案

要控制引入的行,您可以添加TextWatcher,以便用户每次引入新值时都可以处理错误(例如,删除第三个行之后的行)并敬酒).您可以使用\ r和\ n检查新行.

To control the lines introduced you can add a TextWatcher so anytime the user introduces a new value you can handle an error (for example delete the lines after the third one and show a toast). You can check new lines with \r and \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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:38