开发者

How to hide the soft keypad when leaving an editbox?

After a user inputs into an editbox with the keypad, I want the keypad to hide so I can see the results near the bottom of the screen. I've tried this in my onCreate method but it doesn开发者_JAVA技巧't work. What am I missing?

InputMethodManager imm =                                  
      (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditTextbox.getWindowToken(), 0); 


You can force hide the keyboard like so:

Window window = getWindow();
                window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

I would get the window in your onCreate method and in the onFocusChanged() method or OnKeyListener() hide the keyboard.


Have you set up a Key Listener?

You don't really state how you know the user is done entering the text so I'll assume they are pressing the enter button on the soft keyboard. Here is how I am handling that type of scenario. I am using this both in a Dialog and an Activity with success. Hope it helps.

this.setOnKeyListener(new OnKeyListener()
{
     /**
      * This listens for the user to press the enter button on 
      * the keyboard and then hides the virtual keyboard
      */
     @Override
     public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
     {
        // If the event is a key-down event on the "enter" button
        if ( (event.getAction() == KeyEvent.ACTION_DOWN  ) &&
             (keyCode           == KeyEvent.KEYCODE_ENTER)   )
        {               
           // hide virtual keyboard
           InputMethodManager imm = 
              (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
           imm.hideSoftInputFromWindow(sessionTag.getWindowToken(), 0);
           return true;
        }
        return false;
     }
  }); 


It depends on what currently has focus...if its another editText that takes focus then this might be bringing up the keypad...try to explicitly give focus to a different element...


You can extend EditText class and use your code in onFocusChanged() method when focused argument is false.


Follow this answer. call the method after you setContentView in your activity.

https://stackoverflow.com/a/11656129/1066839

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜