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


当前回答

或者你可以在xml中使用focusable标签。

Android:focusable=“false”

设置为false。下面是代码片段

其他回答

您可以尝试为每个元素设置唯一属性

TextView mtextView = findViewById(R.id.myTextView);
mtextView.setShowSoftInputOnFocus(false);

当元素是焦点时,键盘将不显示

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

我认为以下方法可能有用

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

我以前用过这种方法。

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

@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 26和Kotlin有效

   override fun onResume() {
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
    super.onResume()
}