2012-10-23 190 views
14

我有一個應用程序,並且需要關閉的軟鍵盤上的一個相當大量的動作。例如,當點擊按鈕,當一個新的佈局繪製,上屏幕取向變化,當控制器告訴的UI來,等等。 我使用optionsMenuButton用ViewFlipper翻轉視圖,顯然我想讓鍵盤隱藏在翻轉視圖中(這裏沒有輸入字段)。可靠隱藏軟鍵盤

到目前爲止,我已經試過這些,告訴爲什麼這些都是不可靠的:

這一個沒有工作,因爲我有很多editTexts的,和其他的看法。如果可能的話,我需要一個更通用的,不需要視圖作爲參數的。

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

這一個不適合我在所有的工作:

getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

這一個工作,但當視圖被翻轉立即再次會彈出鍵盤。

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); 
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); 

這個有時會起作用,但getCurrentFocus()大部分時間返回null。

InputMethodManager inputManager = (InputMethodManager)    
Context.getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),  
InputMethodManager.HIDE_NOT_ALWAYS); 

這一個工程只有在顯示的鍵盤:

getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_BACK); 

這一個不與EditText上的工作方式類似於第一段代碼,但與根佈局,這與方向的變化而變化每次調用時都會被調用。對於橫向/縱向和正常/大型,我有不同的佈局XML。所有根目錄佈局都有ID root。這很適合第一次,但之後,它不再工作。底線:我已經嘗試了很多軟鍵盤隱藏方法,但沒有一個似乎可靠地工作。 是否有任何可靠隱藏軟鍵盤的方法?

回答

6

因爲你需要一個EditText來調出鍵盤,找到特定的一種,使用第一種方法,你顯示隱藏鍵盤:

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

但是,您將需要EditText。首先,獲取所有視圖:

public static ArrayList<View> getAllViewsFromRoots(View...roots) { 
    ArrayList<View> result = new ArrayList<View>(); 
    for (View root : roots) { 
     getAllViews(result, root); 
    } 
    return result; 
} 

private static void getAllViews(ArrayList<View> allviews, View parent) { 
    allviews.add(parent); 
    if (parent instanceof ViewGroup) { 
     ViewGroup viewGroup = (ViewGroup)parent; 
     for (int i = 0; i < viewGroup.getChildCount(); i++) { 
      getAllViews(allviews, viewGroup.getChildAt(i)); 
     } 
    } 
} 

然後,獲得一個可見的EditText

public static EditText getEditText(View root) { 
    ArrayList<View> views = getAllViewsFromRoots(root); 
    for (View view : views) { 
     if (view instanceof EditText && view.getVisibility() == View.VISIBLE) { 
      return (EditText) view; 
     } 
    } 
    return null; 
} 

某處電話:

Toolkit.getEditText(((ViewParent) findViewById(android.R.id.content)).getChildAt(0)); 

與,調用隱藏方法。

+0

經過一番測試,這似乎是最可靠的方法。 – stealthjong

+0

你能發送代碼示例嗎?在哪裏我應該把Toolkit.getEditText(((ViewParent)findViewById(android.R.id.content))。getChildAt(0)); ? – eyal

+0

嫁給我!我一直在尋找一個很好的解決方案。似乎我找到了一個很好的(而且很短)。謝謝! –

4

這一次爲我的作品總是:

InputMethodManager im = (InputMethodManager) getSystemService(MyActivity.INPUT_METHOD_SERVICE); 
im.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
+0

不適合我,有時適用,90%不適用 – stealthjong

9

我認爲,如果你處理getCurrentFocus()的空情況下,你是好去。我使用下面的方法,它就像一個魅力!

 /* Hide Keyboard */ 
    public static void hideKeyboard(Activity activity){ 
     InputMethodManager inputMethodManager = (InputMethodManager)activity 
       .getSystemService(Context.INPUT_METHOD_SERVICE); 
     View focus = activity.getCurrentFocus(); 
     if(focus != null) 
      inputMethodManager.hideSoftInputFromWindow(focus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
    } 
+1

使用viewflipper時,View焦點爲空,因此鍵盤不會隱藏。可能是viewFlipper的問題? 編輯:可能不是,旋轉屏幕不隱藏鍵盤 – stealthjong

+0

您確定您正在傳遞正確的活動並在正確的地方調用方法嗎?我認爲如果鍵盤打開,View焦點不應該爲空。 – ayorhan

0

只有這個解決方案爲我工作:

mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

我曾與輸入字段對話框,隱藏對話框didnt隱藏平板電腦鍵盤。

0

這應該適用於大多數情況。

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(new View(this).getWindowToken(), 0); 

不需要擔心空指針。