EditText maxLines not working - user can still inp

2019-01-04 07:43发布

<EditText 
    android:id="@+id/editText2" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:maxLines="5" 
    android:lines="5">
</EditText>

User can input more than 5 lines, by pressing enter/next row key. How can I limit user input to fixed amount of rows with EditText?

16条回答
甜甜的少女心
2楼-- · 2019-01-04 08:00

This does not solve the general issue of limiting to n lines. If you want to limit your EditText to take just 1 line of text, this can be very easy.
You can set this in the xml file.

android:singleLine="true"

or programmatically

editText.setSingleLine(true);
查看更多
老娘就宠你
3楼-- · 2019-01-04 08:00

Another way to limit your EditText to one line is the following:

editText2.setTransformationMethod(new SingleLineTransformationMethod());

Note that after applying this transformation method, the enter key creates spaces when pressed. That still satisfies TS' question.

查看更多
别忘想泡老子
4楼-- · 2019-01-04 08:01

this is one approach. Might help someone.

android:lines="1"
android:maxLines="1"
android:inputType="text
查看更多
聊天终结者
5楼-- · 2019-01-04 08:01

Simplest solution:

android:maxLines="3"

...

 @Override
public void afterTextChanged(Editable editable) {
    // limit to 3 lines
    if (editText.getLayout().getLineCount() > 3)
        editText.getText().delete(editText.getText().length() - 1, editText.getText().length());
}
查看更多
神经病院院长
6楼-- · 2019-01-04 08:03

You can limit your text according to your no of lines i say around 37 alphabets in one line

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:lines="4"
    android:maxLines="4"
    android:minLines="4"
    android:maxLength="150"
    android:gravity="start"
    android:background="#efeef5"
    android:layout_marginTop="@dimen/pad_10dp"/>
查看更多
男人必须洒脱
7楼-- · 2019-01-04 08:03
        <EditText
            android:id="@+id/usrusr"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:lines="1"
            android:maxLines="1"
            android:inputType="text"
            android:hint="@string/inventory_no" />
查看更多
登录 后发表回答