android set hidden the keyboard on press enter (in a EditText)

前端 未结 5 1172
醉酒成梦
醉酒成梦 2021-01-31 04:20

When my user press Enter on the virtual android \"user validate entry!\" keyboard my keyboard stay visible! (Why?)

Here my Java code...

privat         


        
5条回答
  •  旧时难觅i
    2021-01-31 04:42

    This should do it:

    yourEditTextHere.setOnEditorActionListener(new OnEditorActionListener() {
    
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                    KeyEvent event) {
                if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    
                    // NOTE: In the author's example, he uses an identifier
                    // called searchBar. If setting this code on your EditText
                    // then use v.getWindowToken() as a reference to your 
                    // EditText is passed into this callback as a TextView
    
                    in.hideSoftInputFromWindow(searchBar
                            .getApplicationWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                   userValidateEntry();
                   // Must return true here to consume event
                   return true;
    
                }
                return false;
            }
        });
    

提交回复
热议问题