Disable Enter in editText android

耗尽温柔 提交于 2019-12-04 23:04:37

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.

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"

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.

You may use

<EditText 
   ......
   android:imeOptions="actionNext" OR "actionDone" />

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

Please read about singleLine

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

use android:singleLine="true" property

     <EditText 
       ......
       android:singleLine="true"
       android:id="@+id/lastName"/>

Or if you wanna use java you could do this:

    edittext1.setSingleLine();

This worked for me,

by setting onKeyListener for the editText in my onCreateView -

editText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            return (keyCode == KeyEvent.KEYCODE_ENTER);
        }
});

Edit: this may not work if you are using Android default keyboard

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!