在SearchView上启动Activity时隐藏键盘无效。

3

我有这样一个情况,当我打开SearchActivity时,我希望在不打开键盘的情况下将光标放在SearchView中(保持焦点)。只有当用户按下SearchView时,我才想要显示键盘(光标应该保持不变)。


2
可能是如何在活动开始时隐藏软键盘的重复问题。 - Santanu Sur
当活动开始时,只需隐藏键盘。 - Manohar
6个回答

2
在您的activity.xml文件的主布局中添加以下行:
<LinearLayout
    ...
    android:focusableInTouchMode="true"
    ...
   >

SearchView具有焦点,但键盘仍然出现。 - MaaAn13

0
将以下代码添加到您的onCreate方法中以隐藏键盘:
public void hideSoftKeyboard() {
    if(getCurrentFocus()!=null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}

0

你使用了以下代码来显示键盘...

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT);

并关闭使用了这个键盘...

        imm.hideSoftInputFromWindow(searchView.getWindowToken(),0);

0
将此行添加到SearchActivity内的Manifest中。
<activity name=".SearchActivity"
android:windowSoftInputMode="stateHidden" />

在Activity中,可以在onCreate()方法中添加。
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

SearchView 已经获得了焦点,但是键盘仍然出现。 - MaaAn13
我已更新答案,请尝试使用stateHidden或code方法。 - Rajan Kali

0

在你的SearchActivityonCreate中尝试这个

 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)‌​;

0
当SearchView获得焦点后,您需要隐藏软键盘:
search.postDelayed({
        val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view?.windowToken, 0)
    }, 100)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接