How to hide keyboard on enter key

后端 未结 2 483
-上瘾入骨i
-上瘾入骨i 2021-02-15 00:22

i have an activity with four edittext and i want to hide keyboard when users finish to use one of the four edittext. if i click enter on keyboard, it will focus another edittext

相关标签:
2条回答
  • 2021-02-15 00:36

    The Best Way To Do This, Is To Add Following Codes In XML File

    android:imeOptions="actionDone"
    

    I am not understand why people suggestion java codes if they have better option

    0 讨论(0)
  • 2021-02-15 00:40

    I am not sure but you should try this code:-

    youredittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    
        @Override
        public boolean onEditorAction(TextView v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                // hide virtual keyboard
                InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(youredittext.getWindowToken(), 0);
                return true;
            }
            return false;
        }
    });
    

    I hope this will help..

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