我有这样一个情况,当我打开SearchActivity
时,我希望在不打开键盘的情况下将光标放在SearchView
中(保持焦点)。只有当用户按下SearchView
时,我才想要显示键盘(光标应该保持不变)。
我有这样一个情况,当我打开SearchActivity
时,我希望在不打开键盘的情况下将光标放在SearchView
中(保持焦点)。只有当用户按下SearchView
时,我才想要显示键盘(光标应该保持不变)。
activity.xml
文件的主布局中添加以下行:<LinearLayout
...
android:focusableInTouchMode="true"
...
>
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
你使用了以下代码来显示键盘...
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT);
并关闭使用了这个键盘...
imm.hideSoftInputFromWindow(searchView.getWindowToken(),0);
SearchActivity
内的Manifest
中。<activity name=".SearchActivity"
android:windowSoftInputMode="stateHidden" />
onCreate()
方法中添加。getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
SearchView
已经获得了焦点,但是键盘仍然出现。 - MaaAn13在你的SearchActivity
的onCreate
中尝试这个
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
search.postDelayed({
val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view?.windowToken, 0)
}, 100)