2011-02-24 52 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; 
    } 
    });