Disable Enter in editText android

2019-02-23 09:15发布

I have a EditText but I want only one line. I put lime this

       <item name="android:maxLines">1</item>
        <item name="android:minLines">1</item>
        <item name="android:lines">1</item>

but doesn't work.

enter image description here

See my code:

<LinearLayout style="@style/linearInputEntryText">
                <LinearLayout  style="@style/subLinearLayout">
                    <TextView style="@style/label" android:text="Last Name" android:id="@+id/textView2"/>
                    <EditText style="@style/editTextEntryName" android:id="@+id/lastName"/>
                </LinearLayout>
                <View style="@style/linearInputEntryBlack" > </View>
            </LinearLayout>

and styles:

    <style name="editTextEntryName" parent="editTextEntry">
    <item name="android:maxLines">1</item>
    <item name="android:minLines">1</item>
    <item name="android:lines">1</item>
    <item name="android:layout_gravity">left</item>
</style>

8条回答
欢心
2楼-- · 2019-02-23 09:19

Because of different Android versions, the best practice is to include these parameters at the same time:

<item name="android:maxLines">1</item>
<item name="android:lines">1</item>
<item name="android:singleLine">true</item>

If you don't, it will not work correctly on some devices.

查看更多
forever°为你锁心
3楼-- · 2019-02-23 09:20

Please use

android:singleLine="true"

instead of

    <item name="android:maxLines">1</item>
    <item name="android:minLines">1</item>
    <item name="android:lines">1</item>

That single attribute is enough to make your EditText accept only one line of text and disable the enter button.

UPDATE

Don't worry about this comment,

android:singleLine is deprecated it's not a good practice using it

This attributed is officially supported and is not deprecated in any possible way. And it is also guaranteed that this will work in all devices and on all Android versions.

查看更多
狗以群分
4楼-- · 2019-02-23 09:31

android:maxLines="1" allows the text to be multiline but limits the height of edittext to display a single line.

If you want to disable the enter (new line) key, set the input type to text

    android:inputType="text"
查看更多
干净又极端
5楼-- · 2019-02-23 09:38

Use android:singleLine=true instead android:maxLines=1

Please read about singleLine

http://developer.android.com/reference/android/widget/TextView.html#attr_android:singleLine

查看更多
forever°为你锁心
6楼-- · 2019-02-23 09:39

use android:singleLine="true" property

     <EditText 
       ......
       android:singleLine="true"
       android:id="@+id/lastName"/>
查看更多
Emotional °昔
7楼-- · 2019-02-23 09:39

You can set the input type to textEmailSubject. I head the same issue and it solve it!

查看更多
登录 后发表回答