我想在 EditText 字段可见性模式下获得软键盘 (IME) 状态?
I want to get softkeyboard (IME) status in EditTextField visibility mode?
我尝试获取软键盘状态(显示或隐藏)
使用 onConfigurationChanged(此处:http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange)
但这对我不起作用。我不知道。
(我也已经尝试过 hardKeyboardHidden 和 keyboard-config 的值)
请检查我的代码。
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:configChanges="keyboardHidden"
android:windowSoftInputMode="adjustResize"
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity.java
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks whether a hardware keyboard is available
if (newConfig.keyboardHidden== Configuration.HARDKEYBOARDHIDDEN_NO) {
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.keyboardHidden== Configuration.HARDKEYBOARDHIDDEN_YES) {
Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
}
}
并且只是简单地在 activity_main.xml
中添加了 EditText
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
那么如何解决这个问题?
此方法使用onMeasure()
。它检查 activity 屏幕是否较小。
How to check visibility of software keyboard in Android?
下面的解决方案呢?
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
writeToLog("Software Keyboard is visible");
} else {
writeToLog("Software Keyboard is not visible");
}
我尝试获取软键盘状态(显示或隐藏) 使用 onConfigurationChanged(此处:http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange)
但这对我不起作用。我不知道。 (我也已经尝试过 hardKeyboardHidden 和 keyboard-config 的值)
请检查我的代码。
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:configChanges="keyboardHidden"
android:windowSoftInputMode="adjustResize"
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity.java
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks whether a hardware keyboard is available
if (newConfig.keyboardHidden== Configuration.HARDKEYBOARDHIDDEN_NO) {
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.keyboardHidden== Configuration.HARDKEYBOARDHIDDEN_YES) {
Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
}
}
并且只是简单地在 activity_main.xml
中添加了 EditText<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
那么如何解决这个问题?
此方法使用onMeasure()
。它检查 activity 屏幕是否较小。
How to check visibility of software keyboard in Android?
下面的解决方案呢?
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
writeToLog("Software Keyboard is visible");
} else {
writeToLog("Software Keyboard is not visible");
}