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


当前回答

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

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

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

其他回答

扩展一下@Lucas的回答:

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

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

芬兰湾的科特林的例子:

override fun onResume() {
  super.onResume()

  window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
}
//to hide the soft keyboard
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

为所有使用该主题的活动隐藏它

<style name="MyTheme" parent="Theme">
    <item name="android:windowSoftInputMode">stateHidden</item>
</style>

设定主题

<application android:theme="@style/MyTheme">

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

像这样:

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

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

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