Pergunta

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)
Foi útil?

Solução

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);

Outras dicas

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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top