活动开始时,Kotlin Android搜索菜单项将变为焦点

时间:2018-07-08 09:05:41

标签: android button search kotlin

问题:活动开始时如何使搜索菜单项聚焦? 我希望活动从下面的第二张图片开始。

以下是我的代码:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_search_qpon)
    setSupportActionBar(qponSearch_toolbar)
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the menu to use in the action bar
    menuInflater.inflate(R.menu.search_menu, menu)

    val searchItem = menu.findItem(R.id.app_bar_search)

    if (searchItem!=null) {
        val searchView = searchItem.actionView as SearchView

        val searchHint = getString(R.string.searchHint)
        searchView.setQueryHint(searchHint)

        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText!!.toString().isNotEmpty()) {

                    startRecyclerView(generateData(newText))
                    companyList.clear()
                }
                else {
                    startRecyclerView(generateData(newText))
                    companyList.clear()
                }
                return false
            }
        })
    }

    return super.onCreateOptionsMenu(menu)
}

以下是xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/bg"
    android:orientation="vertical"
    tools:context=".SearchQponActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/qponSearch_toolbar"
        android:layout_width="match_parent"
        app:title=""
        android:layout_height="56dp"
        android:background="@color/white50"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/qponSearch_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </android.support.v7.widget.RecyclerView>

</LinearLayout>

有关MENU文件:

<?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/app_bar_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="Search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always" />


</menu>

第一个图像,这是活动开始时现在显示的图像: enter image description here

第二张图片,然后单击搜索按钮,它显示: enter image description here

1 个答案:

答案 0 :(得分:1)

您必须在expandActionView()方法中调用searchItem的{​​{1}}方法。检查下面给出的代码:

onCreateOptionsMenu()

此外,您还必须更改菜单布局XML文件。您必须将showAsAction的属性从override fun onCreateOptionsMenu(menu: Menu?): Boolean { menuInflater.inflate(R.menu.test_menu, menu) val searchItem = menu?.findItem(R.id.app_bar_search) searchItem?.expandActionView() // This line will expand the search view. .... } 更改为always。检查下面给出的代码。

ifRoom|collapseActionView