我有一个编辑文本输入的活动。当activity初始化时,显示Android键盘。在用户聚焦输入之前,键盘如何保持隐藏?


当前回答

只要在你的活动中添加这个:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
      if (getCurrentFocus() != null) {
           InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
           imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
      }
      return super.dispatchTouchEvent(ev);
}

其他回答

如果你使用的是API级别21,你可以使用editText.setShowSoftInputOnFocus(false);

函数隐藏键盘。

public static void hideKeyboard(Activity activity) {
    View view = activity.getCurrentFocus();

    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

隐藏键盘在androidmanifest .xml文件。

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:windowSoftInputMode="stateHidden">

我认为以下方法可能有用

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

我以前用过这种方法。

(android:windowSoftInputMode="stateAlwaysHidden") 在活动标签内的清单中。

像这样:

<activity android:name=".MainActivity"
  android:windowSoftInputMode="stateAlwaysHidden">

扩展一下@Lucas的回答:

从生命周期早期事件的活动中调用这个:

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

芬兰湾的科特林的例子:

override fun onResume() {
  super.onResume()

  window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
}