restrict edittext to single line

2020-01-25 03:41发布

possible duplicate : android-singleline-true-not-working-for-edittext

<EditText 
    android:id="@+id/searchbox"  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:lines="1"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:layout_weight="1"
    android:layout_marginTop="2dp"
    android:drawablePadding="10dp"
    android:background="@drawable/edittext"
    android:drawableLeft="@drawable/folder_full"
    android:drawableRight="@drawable/search"
    android:paddingLeft="15dp"
    android:hint="search...">
</EditText>

I want to make the above EditText to have only single line. Even if the user presses "enter" the cursor should not get down to the second line. Can anybody help me doing that?

23条回答
SAY GOODBYE
2楼-- · 2020-01-25 04:15

in XML File add this properties in Edit Text

 android:maxLines="1"
查看更多
劫难
3楼-- · 2020-01-25 04:17

You must add this line in your EditText in xml:

android:maxLines="1"
查看更多
混吃等死
4楼-- · 2020-01-25 04:18
android:imeOptions="actionDone"

worked for me.

查看更多
\"骚年 ilove
5楼-- · 2020-01-25 04:19

Using android:singleLine="true" is deprecated.

Just add your input type and set maxline to 1 and everything will work fine

android:inputType="text"
android:maxLines="1"
查看更多
神经病院院长
6楼-- · 2020-01-25 04:20

please try this code

android:inputType="textPersonName"
查看更多
登录 后发表回答