隐藏储物柜应用程序的软键盘

Hide softkeyboard for locker app

我正在尝试关闭在另一个应用程序中打开的软键盘。 我从这里尝试了所有解决方案: Programmatically Hide/Show Android Soft Keyboard or here: Close/hide the Android Soft Keyboard

正如您在图片中看到的那样,我必须关闭从另一个应用程序打开的键盘,添加到清单以不让键盘可见并没有成功。

请注意,这是一个储物柜应用程序,当 phone 进入睡眠模式时,我会启动一个 activity。

我错过了什么吗?从商店测试其他储物柜应用程序并没有遇到这个问题

但这是结果:

编辑:更多信息

这是我启动储物柜的方式:

if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
    //Toast.makeText(context, "" + "screeen off", Toast.LENGTH_SHORT).show();

    wasScreenOn = false;
    Intent intent = new Intent(context, LockScreenActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    context.startActivity(intent);

    // do whatever you need to do here
    //wasScreenOn = false;
} 

这是清单代码:

<activity
    android:name=".ui.activities.LockScreenActivity"
    android:excludeFromRecents="true"
    android:noHistory="true"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateAlwaysHidden|adjustNothing"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

Try replacing android:windowSoftInputMode="stateAlwaysHidden|adjustNothing" with android:windowSoftInputMode="stateHidden" line in AndroidManifest.xml like this

<activity
        android:name=".ui.activities.LockScreenActivity"
        android:excludeFromRecents="true"
        android:noHistory="true"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

参考可以参考http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

"stateHidden" The soft keyboard is hidden when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity.

"stateAlwaysHidden" The soft keyboard is always hidden when the activity's main window has input focus.

可以覆盖onPause()这个activity并使用下面的代码作为

@Override
public void onPause() {
    super.onPause();
    if (null != getWindow()){
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }
}

在您的 Activity 中试试这个:

private void hideKeyboard() {   
    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

试试这个方法

  InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(
                        Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

Check this link

我终于解决了这个问题。这就是我的 activity 清单代码的样子:

<activity
        android:name=".ui.activities.LockScreenActivity"
        android:excludeFromRecents="true"
        android:noHistory="true"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden"
        android:configChanges="keyboardHidden"
        android:launchMode="singleInstance"
        android:multiprocess="false"
        android:stateNotNeeded="true"
        android:taskAffinity=""
        android:theme="@style/Theme.AppCompat.Light.NoActionBar" />