简体   繁体   中英

Android- Force visible soft keyboard

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM