java - 软键盘在片段之间不断打开
问题描述
每次我导航到应用程序中的新片段时,都会显示软键盘。我目前在做什么:我有打开片段 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,所以我认为这不是焦点问题。这是我尝试过的:
把它放在 AndroidManifest.xml 中:
安卓:windowSoftInputMode="stateHidden"
将此添加到每个片段的 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>
解决方案
public void setHideSoftKeyboard(View view) {
InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mInputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
当您从 Fragment A 打开 Fragment B 时调用此方法,并将任何视图传递给此方法,以便它可以关闭键盘。
推荐阅读
- javascript - 参数化 Cucumber 中的功能
- python-3.x - PyCMS:ICC 配置文件不能转换
- windows - Powershell 无法将我的变量读取为启动进程文件路径
- c++ - Clion Qt 缺少 DLL 库
- node.js - 使用 Node.JS 制作的 Socket.IO 聊天示例在“传输模式”=websocket 下无法在 Azure 上运行
- performance - 有没有一种方法可以在 x86 中并行测量多个负载的延迟(使用 RDTSCP 或 RDTSC),而无需序列化?
- dns - Netlify 部署在 Gatsby 构建时失败
- google-apps-script - 从 G Suite 云端硬盘插件中获取当前网址(或云端硬盘文件夹 ID)的方法?
- python - 在算法复杂度优于线性的列表中找到第一个偶数
- python - 将毫秒转换为帧数(每秒 25 帧)