本文介绍了在Kotlin中自动扩展searchView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 IndexActivity .单击searchView后,将启动一个名为 SearchActivity .再次在搜索活动中,我得到了searchView,它会在单击时扩展.它有一个查询侦听器,它可以进行外部API调用.

I have a searchView in my IndexActivity. On clicking the searchView a new activity is started called SearchActivity.Inside the search activity again, I have the searchView which expands on clicking. It has a query listener which does an external API call.

但是,在通过单击searchView图标打开SearchActivity之后,我必须再次单击searchView进行键入.我想要的是searchView应该在活动开始时立即关注.这是我的搜索活动代码:

However, after I open the SearchActivity by clicking on searchView icon, I have to again click on searchView to type in it. What I want is searchView should be focussed as soon as the activity starts.This is my code of searchactivity:

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.toolbar_search, menu)
        val searchView: SearchView =
            menu?.findItem(R.id.menu_search)?.actionView as SearchView
        // searchView.setIconifiedByDefault(false)
        // searchView.focusable = View.FOCUSABLE
        // searchView.isIconified = false
        // searchView.requestFocusFromTouch()
        // searchView.onActionViewExpanded()

带注释的行是我在网上搜索时尝试过的行,但似乎不起作用.

The commented lines are the ones I tried by searching on the web but it doesn't seem to work.

这是我在toolbar_searchbar.xml中的代码,该代码包含在SearchActivity设置的activity_search中:

This is my code in toolbar_searchbar.xml which is included in activity_search set by SearchActivity:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:titleTextColor="@color/cardview_light_background"
            app:subtitleTextAppearance="@font/alegreya_sans_medium"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:background="@android:color/transparent"
            android:id="@+id/toolbar_searchbar"
            app:popupTheme="@style/AppTheme.PopupOverlay">
        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

这是toolbar_search.xml的代码,如上面提供的代码所示,该代码被夸大了:

This is the code of toolbar_search.xml which is inflated as seen in the code provided above:

<?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:title="Search"
        android:id="@+id/menu_search"
        android:icon="@drawable/quantum_ic_search_grey600_24"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        />

</menu>

这就是我刚开始的时候.

This is what I get at the start.

这是我一开始想要的.

推荐答案

请确保导入:androidx.appcompat.widget.SearchView

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/search_contacts"
        android:title="search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always"
        app:actionViewClass="androidx.appcompat.widget.SearchView" >
    </item>
</menu>
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.search_view, menu)
    val searchView: SearchView =
        menu?.findItem(R.id.search_contacts)?.actionView as SearchView

    searchView.isIconified = false
    return true
}

这篇关于在Kotlin中自动扩展searchView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 05:12