Android: Remove Enter Key from softkeyboard

天大地大妈咪最大 提交于 2019-12-03 16:32:36

问题


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 previous place and prints a dot in the password field (which is the next field).

I want to remove this enter key from the softkeyboard. Is it possible?


回答1:


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




回答2:


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

And your ENTER key is gone




回答3:


add this tag to textView in xml

    android:singleLine = "true"



回答4:


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.




回答5:


New update :

android:maxLines="1"



来源:https://stackoverflow.com/questions/6991301/android-remove-enter-key-from-softkeyboard

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