软键盘不断在片段之间打开

时间:2018-07-09 21:26:05

标签: java android android-softkeyboard

每次我在应用程序中导航到一个新片段时,都会显示软键盘。我目前正在做什么:我有打开片段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();
}

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

  1. 将其放入AndroidManifest.xml中:

    android:windowSoftInputMode =“ stateHidden”

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

    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>

1 个答案:

答案 0 :(得分:1)

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

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