首页 > 解决方案 > 软键盘在片段之间不断打开

问题描述

每次我导航到应用程序中的新片段时,都会显示软键盘。我目前在做什么:我有打开片段 B 的片段 A,我把它放在片段 A 中:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    AndroidUtils.closeKeyboard(activity); //custom method I use to close the keyboard
}

在片段 B 中:

@Override
public void onStop() {
    AndroidUtils.closeKeyboard(activity);
    super.onStop();
}

我在这方面取得了最大的成功,但问题是键盘会在过渡期间打开,然后在片段 B 打开时关闭。我不希望键盘出现 - 这些片段都没有 EditTexts,所以我认为这不是焦点问题。这是我尝试过的:

  1. 把它放在 AndroidManifest.xml 中:

    安卓:windowSoftInputMode="stateHidden"

  2. 将此添加到每个片段的 OnCreateView() 甚至 OnCreate():

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

这是一个非常奇怪的错误,有什么想法吗?

编辑:我的 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="App.App.AppName">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-sdk android:minSdkVersion="16" />

<application
    android:name=".Global"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:windowSoftInputMode="stateHidden">
    <activity
        android:name=".AppName.MainActivity"
        android:configChanges="orientation"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".firebase.DefaultFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

</application>

标签: javaandroidandroid-softkeyboard

解决方案


public void setHideSoftKeyboard(View view) {
        InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mInputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

当您从 Fragment A 打开 Fragment B 时调用此方法,并将任何视图传递给此方法,以便它可以关闭键盘。


推荐阅读