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


当前回答

只有这个解决方案对我的API 26和Kotlin有效

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

其他回答

对我来说最好的解决办法是,粘贴你的类

@Override
public void onResume() {
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    super.onResume();
}

@Override
public void onStart() {
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    super.onStart();
}

试试这个——

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

另外,

您还可以在清单文件中声明activity -

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
          android:label="@string/app_name"
          android:windowSoftInputMode="stateHidden"
          >

如果你已经使用android:windowSoftInputMode的值,如adjuststresize或adjustPan,你可以组合两个值,如:

<activity
        ...
        android:windowSoftInputMode="stateHidden|adjustPan"
        ...
        >

这将在适当的时候隐藏键盘,但在必须显示键盘的情况下平移活动视图。

将这两个属性添加到你的父布局(例如:线性布局,相对布局)

android:focusable="false"
android:focusableInTouchMode="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">

只有这个解决方案对我的API 26和Kotlin有效

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