Disable Enter in editText android

前端 未结 10 1661
别那么骄傲
别那么骄傲 2021-02-05 03:15

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

1


        
相关标签:
10条回答
  • 2021-02-05 03:37

    use android:singleLine="true" property

         <EditText 
           ......
           android:singleLine="true"
           android:id="@+id/lastName"/>
    
    0 讨论(0)
  • 2021-02-05 03:41

    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.

    0 讨论(0)
  • 2021-02-05 03:43

    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.

    0 讨论(0)
  • 2021-02-05 03:47

    Use xml attribute

    <EditText
        ......
        android:inputType="text" />
    
    0 讨论(0)
  • 2021-02-05 03:48

    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

    0 讨论(0)
  • 2021-02-05 03:50

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

    Please read about singleLine

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

    0 讨论(0)
提交回复
热议问题