2017-10-20 22 views
0

後,我調用的方法是這樣的:鍵盤將不顯示主叫InputMethodManager.showSoftInput

getInputMethodManager().showSoftInput(view, 0, resultReceiver); 

但是,鍵盤不顯示我的屏幕上,並resultReceiver不onReceiveResult(int resultCode, Bundle resultData)收到一條消息。該視圖不爲空,並且hasFocus() == true

有沒有人知道這個問題?非常感謝。

謝謝Karthik和Imen。我需要知道爲什麼這種方法不起作用的根本原因,但不是一些解決方法。

更新於2017年10月23日

我debuged到Android SDK中的源代碼,並發現:一個名爲mServedView變量爲空並且該方法將返回一個false。然而,[官方文檔](https://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html#showSoftInput(android.view.View,int,android.os.ResultReceiver)不會告訴爲什麼或何時此方法將返回false

+0

KARTHIK,謝謝您的回答但它不能解決我的問題。在我的情況下,我不想使用'toggleSoftInput'而不是'showSoftInput',並且我不能使用'InputMethodManager.SHOW_FORCED',因爲它是一種util方法。標誌需要傳入此方法。 – freshomer

回答

1

嘗試這樣的:

爲了顯示鍵盤:

public void showSoftKeyboard(Context ctx, View v) { 
     InputMethodManager imm = (InputMethodManager) ctx.getSystemService(
       Context.INPUT_METHOD_SERVICE); 
     imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 
    } 

隱藏鍵盤:

public void hideSoftKeyboard(Context ctx, View v) { 
     InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(
       Context.INPUT_METHOD_SERVICE); 
     inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0); 
    } 
0

試試這個方法:

public void showSoftKeyboard(View aView) { 
     if (aView != null) { 
      aView.setFocusable(true); 
      aView.setFocusableInTouchMode(true); 
      aView.requestFocus(); 
      InputMethodManager keyboard = (InputMethodManager) 
        getSystemService(Context.INPUT_METHOD_SERVICE); 
      keyboard.showSoftInput(aView, 0); 
     } 
    }