Android: Remove Enter Key from softkeyboard

前端 未结 6 1393
余生分开走
余生分开走 2021-02-07 14:39

In my login form when user clicks on an EditText and presses the enter key, this inserts a new line, therefore increasing the EditText\'s size. Next moment, it returns to its pr

相关标签:
6条回答
  • 2021-02-07 15:13

    If you want something more generic in your .java:

    boolean state = true; yourTextInputEditText.setSingleLine(state);

    0 讨论(0)
  • 2021-02-07 15:14

    New update :

    android:maxLines="1"

    0 讨论(0)
  • 2021-02-07 15:19

    I am afraid you can't do this. But one thing is you can handle the softkeyboard keyevents like this,

    edittext.setOnKeyListener(new OnKeyListener() {
    
            public boolean onKey(View v, int keyCode, KeyEvent event) {
    
    
                    if (event.getAction() == KeyEvent.ACTION_DOWN
                            && event.getKeyCode() ==       KeyEvent.KEYCODE_ENTER) {
                        Log.i("event", "captured");
    
                        return false;
                    } 
                    else if(event.getAction() == KeyEvent.ACTION_DOWN
                            && event.getKeyCode() == KeyEvent.KEYCODE_BACK){
                        Log.i("Back event Trigered","Back event");
    
                    }
    
                }
    
                }
                return false;
            }
        });
    

    Apart from this, you have to note that providing the attribute android:singleLine=true will make your edittext from growing in size when the soft keyborad ENTER is pressed

    0 讨论(0)
  • 2021-02-07 15:22

    add this tag to textView in xml

        android:singleLine = "true"
    
    0 讨论(0)
  • 2021-02-07 15:32

    Use : android:singleLine = "true" or edittext.setSingleLine();

    And your ENTER key is gone

    0 讨论(0)
  • 2021-02-07 15:32

    Inside the tag EditText you only have to do:

    android:singleLine="true"
    

    this remove the enter key in the keyboard

    UPDATE

    Inasmuch as android:singleLine="true" is deprecated I use android:maxLines="1" to avoid the enter in a EditText. How the name of the method says only N lines is permitted.

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