i've an edittext that when I click on enter (not button) , the soft keyboard disappears. How can i do to close the soft keyboard only with BACK button, and not with other events?

if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)
有帮助吗?

解决方案

The following code may show the keyboard programmatically. Just catch the KeyEvent as you are doing in your question code and place this code inside that function:

yourEditText.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            InputMethodManager keyboard = (InputMethodManager)
            getSystemService(Context.INPUT_METHOD_SERVICE);
            keyboard.showSoftInput(mUserNameEdit, 0);
        }
    },0);

I do not recommend using InputMethodManager.SHOW_FORCED because it can cause strange layout issues. I would rather use this snippet in that case:

InputMethodManager mgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

其他提示

Put this code in the onCreate() of your activity.

InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top