在 activity 打开 android 时设置 searchView 聚焦

Set searchView focused when activity opens android

我有一个带有包含 searchView 的菜单的 SearchActivity。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/searchMessageMenuItem"
        android:icon="@drawable/ic_search"
        android:title="Search"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="androidx.appcompat.widget.SearchView" />

</menu>

在 onCreateOptionsMenu 中我得到了 searchView

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.search_message_menu, menu)
        searchView = menu?.findItem(R.id.searchMessageMenuItem)?.actionView as SearchView
        searchView?.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (!query.isNullOrEmpty() && validateQuery(query))
                    searchViewModel.searchMessage(query)
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean { return true }
        })
        return super.onCreateOptionsMenu(menu)
    }

因此,当我单击搜索图标时,键盘出现并且工具栏设置为写入。 好吧,我希望此功能在 activity 启动时默认发生,而不是通过单击搜索图标。我试过 searchView.requestFocus() 但这行不通!

您首先需要在 menu.xml

中设置 app:showAsAction ="always"

并在 onCreateOptionsMenu 上使用 searchView.setIconifiedByDefault(false) 保持 searchView 默认扩展。

这并没有打开键盘,仍然是创建了这个打开键盘的函数:

private fun setFocused(searchView: SearchView) {
    searchView.requestFocus()
    val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    imm.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT)
}

这是完整的代码:

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.search_message_menu, menu)
    searchView = menu?.findItem(R.id.searchMessageMenuItem)?.actionView as SearchView
    searchView.setIconifiedByDefault(false)
    setFocused(searchView)
    searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String?): Boolean {
            /*
             * your code here
             */
            return true
        }

        override fun onQueryTextChange(newText: String?): Boolean {
            return true
        }
    })
    return super.onCreateOptionsMenu(menu)
}


private fun setFocused(searchView: SearchView) {
    searchView.requestFocus()
    val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    imm.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT)
}

这是菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/searchMessageMenuItem"
        android:icon="@android:drawable/ic_menu_search"

        android:title="Search"
        app:showAsAction="always"
        app:actionViewClass="androidx.appcompat.widget.SearchView" />

</menu>

更新

对于始终显示的图标,我发现这个解决方案很有用重绘搜索视图时要隐藏的图标,这是我的解决方案:

 searchView.setIconifiedByDefault(false)


 searchView.viewTreeObserver.addOnDrawListener {
            searchView.findViewById<AppCompatImageView>(androidx.appcompat.R.id.search_button).visibility = View.GONE
            searchView.findViewById<AppCompatImageView>(androidx.appcompat.R.id.search_mag_icon).visibility = View.GONE
        }
 setFocused(searchView)

此外,如果您希望在查询为空时保持关闭图标可见,您可以使用:

searchView.findViewById<AppCompatImageView>(androidx.appcompat.R.id.search_close_btn).visibility = View.VISIBLE