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条回答
倾城 Initia
2楼-- · 2020-01-25 03:55

In order to restrict you just need to set the single line option on "true".

android:singleLine="true"
查看更多
Summer. ? 凉城
3楼-- · 2020-01-25 03:58

Use android:maxLines="1" and android:inputType="text"

You forgot the android:maxLines attribute. And refer for android:inputType With your example, below will give this result:

<EditText 
    android:id="@+id/searchbox"  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:inputType="text"
    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>
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-01-25 03:58

Now android:singleLine attribute is deprecated. Please add these attributes to your EditText for an EditText to be single line.

android:inputType="text"
android:imeOptions="actionNext"
android:maxLines="1"
查看更多
Emotional °昔
5楼-- · 2020-01-25 03:58

Everyone's showing the XML way, except one person showed calling EditText's setMaxLines method. However, when I did that, it didn't work. One thing that did work for me was setting the input type.

EditText editText = new EditText(this);
editText.setInputType(InputType.TYPE_CLASS_TEXT);

This allows A-Z, a-z, 0-9, and special characters, but does not allow enter to be pressed. When you press enter, it'll go to the next GUI component, if applicable in your application.

You may also want to set the maximum number of characters that can be put into that EditText, or else it'll push whatever's to the right of it off the screen, or just start trailing off the screen itself. You can do this like this:

InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(8);
editText.setFilters(filters);

This sets the max characters to 8 in that EditText. Hope all this helps you.

查看更多
Viruses.
6楼-- · 2020-01-25 03:59

You must add this line in your EditText

android:maxLines="1"

and another thing, don't forget set android:inputType (whatever you want, text, phone .., but you must set it)

查看更多
一夜七次
7楼-- · 2020-01-25 04:00

Add this line to your edittext

android:inputType="text"

查看更多
登录 后发表回答