1

我試圖關閉我的自定義鍵盤後單擊gridview.Im試圖在BaseAdapter類中的項目。上下文來自InputMethodService。如何關閉/隱藏自定義鍵盤Android

到目前爲止,我已經試過如下:

FrameLayout scroll = (FrameLayout)inflater.inflate(R.layout.keyboard, null); 
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(scroll.getWindowToken(), 0); 

-

imm.toggleSoftInput(0,InputMethodManager.HIDE_IMPLICIT_ONLY); 

-

scroll.setVisibility(View.INVISIBLE); 

感謝

回答

1

我只是複製和粘貼從我的應用程序在這裏,它適用於我們:

public static void hideKeyboard(View v) { 
     try { 
     v.clearFocus(); 
     InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 
     } catch (Exception e) { 
     // we all saw shit happening on this code before 
     } 
    } 
+0

沒有錯誤,但鍵盤仍然呈現 –

0

您可以將此方法放在公共類中,並在需要時調用它。

public static void hideKeyboard(Context ctx) { 
InputMethodManager inputManager = (InputMethodManager) ctx 
.getSystemService(Context.INPUT_METHOD_SERVICE); 

// check if no view has focus: 
View v = ((Activity) ctx).getCurrentFocus(); 
if (v == null) 
    return; 

inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0); 
} 

參考我的答案here

+0

我得到無法得到施展到Android.app.Activity錯誤,我的上下文類擴展InputMethodService :(我不能使用的活動(如有一種方法我不知道) –

+0

如果你在鍵盤上,通過requestHideSelf隱藏自己,但你的鍵盤可能沒有gridView,所以我覺得你有點困惑。 –