簡體   English   中英

如何關閉/隱藏自定義鍵盤 Android

[英]How to close/hide custom keyboard Android

我嘗試在 gridview 中單擊項目后關閉我的自定義鍵盤。我正在嘗試在 BaseAdapter 類中執行此操作。 上下文來自 InputMethodService。

到目前為止,我已經嘗試過:

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

如果您有自己的自定義鍵盤並且擴展了InputMethodService ,那么您只需調用

requestHideSelf(0)

從您的服務中強制關閉鍵盤或

requestHideSelf(InputMethodManager.HIDE_IMPLICIT_ONLY);

僅當用戶沒有明確要求顯示鍵盤時才關閉鍵盤。

文檔

我只是在這里從我的應用程序復制和粘貼,它對我們來說很好用:

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

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

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

在這里參考我的回答

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM