我有一个带有Edit Text
输入框的活动。当活动被初始化时,安卓键盘会显示出来。如何让键盘保持隐藏状态,直到用户聚焦该输入框?
我有一个带有Edit Text
输入框的活动。当活动被初始化时,安卓键盘会显示出来。如何让键盘保持隐藏状态,直到用户聚焦该输入框?
在@Lucas所接受的答案的基础上,进一步扩展:
在您的活动中的一个早期生命周期事件中调用此函数:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
override fun onResume() {
super.onResume()
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
}
public static void hideKeyboard(Activity activity) {
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:windowSoftInputMode="stateHidden">
//to hide the soft keyboard
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
只需在您的Activity中添加以下内容:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (getCurrentFocus() != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
return super.dispatchTouchEvent(ev);
}
只有这个解决方案在 API 26 和 Kotlin 上对我起了作用。
override fun onResume() {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
super.onResume()
}
在你的活动标签中,在清单文件中声明此代码(android:windowSoftInputMode="stateAlwaysHidden")。
像这样:
<activity android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden">
<activity android:windowSoftInputMode="stateHidden" ...>
表示隐藏软键盘。 - idiottigerandroid:windowSoftInputMode="adjustPan"
一起使用? - János