2011-02-24 108 views
4

用户使用键盘输入到编辑框后,我希望键盘隐藏,以便可以在屏幕底部附近看到结果。我在我的onCreate方法中试过这个,但它不起作用。我错过了什么?如何在离开编辑框时隐藏软键盘?

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

回答

1

这要看什么当前具有焦点...如果它的另一个EDITTEXT这需要集中那么这可能是造就键盘...尽量明确焦点赋予不同的元素...

+0

这是它,因为我失去了重点的EditText框上面显示我的代码隐藏键盘,但重点是针对由此拉开了键盘对我将在未来的EditText框。为了解决这个问题,我输入了两个命令来隐藏键盘,一个用于我离开的edittext框,另一个用于我刚刚关注的编辑框: InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myEditText1.getWindowToken(),0); imm.hideSoftInputFromWindow(myEditText2.getWindowToken(),0); – 2011-02-25 16:15:00

1

你可以扩展EditText类,并使用你的代码onFocusChanged()方法时focused说法是false

3

你可以力隐藏键盘,像这样:

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

我会得到这个窗口的onCreate方法,并在onFocusChanged()方法或OnKeyListener()隐藏键盘。

2

您是否设置了密钥监听器?

你并没有真正说明你是如何知道用户输入文本的,因此我假定他们正按下软键盘上的输入按钮。以下是我如何处理这种情况。我在一个对话框和一个成功的活动中使用它。希望能帮助到你。

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