how to handle back button of Search View in android

前端 未结 4 1238
刺人心
刺人心 2021-02-19 13:24
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(Menus.SEARCH));
searchView.setQueryHint(this.getString(R.string.search));
editSearch = (E         


        
4条回答
  •  暖寄归人
    2021-02-19 13:27

    If you are using search dialog you can do something like this for Kotlin

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
    
        return if (id == R.id.search_button) {
            val searchManager = this.getSystemService(Context.SEARCH_SERVICE) as SearchManager
            searchManager.setOnDismissListener {
                // return the activity to the normal state
            }
            // set activity to search state then request search
            onSearchRequested()
        } else super.onOptionsItemSelected(item)
    
    }
    

提交回复
热议问题