如何处理单击工具栏中 SearchView 中的后退箭头
How to handle clicking back arrow in SearchView in Toolbar
如何处理在 androidx.appcompat.widget.SearchView
中单击后退箭头 ?
我找到了这个答案:How to handle back arrow event in a SearchView 但它对我不起作用。我从片段设置菜单。
在 MainActivity 中我有这个功能:
override fun onOptionsItemSelected(item: MenuItem): Boolean
{
Log.d("MyTag", "Activity")
return super.onOptionsItemSelected(item)
}
override fun onSupportNavigateUp(): Boolean
{
Log.d("MyTag", "Up")
return navController.navigateUp()
}
在片段中:
searchView.setOnQueryTextListener(
object : SearchView.OnQueryTextListener
{
override fun onQueryTextSubmit(p0: String?): Boolean
{
Log.d("MyTag", "onQueryTextSubmit")
hideKeyboard()
return true
}
override fun onQueryTextChange(p0: String?): Boolean
{
return true
}
}
)
override fun onOptionsItemSelected(item: MenuItem): Boolean
{
Log.d("MyTag", "Fragment")
return super.onOptionsItemSelected(item)
}
None 这些函数在我单击后退箭头时被调用。 Logcat 看起来像这样:
// clicking search button
Activity
Fragment
// clicking Enter on keyboard
onQueryTextSubmit
// clicking back arrow (Nothing)
// When I go to the next fragment and navigate back
Activity
Up
为什么我在SearchView中点击后退箭头没有调用任何事件?我怎样才能做到这一点?
感谢 Rustam Samandarov 这里是 onCreateOptionsMenu
的完整代码:
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)
{
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.menu_toolbar_home, menu)
val menuItem = menu.findItem(R.id.itemSearch)
val searchView = menuItem.actionView as SearchView
searchView.setOnQueryTextListener(
object : SearchView.OnQueryTextListener
{
override fun onQueryTextSubmit(p0: String?): Boolean
{
Log.d("MyTag", "onQueryTextSubmit") // called when enter is pressed
hideKeyboard()
return true
}
override fun onQueryTextChange(p0: String?): Boolean
{
return true // called every time when text is changed
}
}
)
menuItem.setOnActionExpandListener(
object : MenuItem.OnActionExpandListener
{
override fun onMenuItemActionExpand(p0: MenuItem?): Boolean
{
Log.d("MyTag", "onMenuItemActionExpand") // called when user opens SearchView
return true;
}
override fun onMenuItemActionCollapse(p0: MenuItem?): Boolean
{
Log.d("MyTag", "onMenuItemActionCollapse") // called when user click back arrow or navigation back button
return true
}
}
)
}
您好,您应该将扩展侦听器设置为搜索视图
searchMenu.setOnActionExpandListener(
object : MenuItem.OnActionExpandListener {
override fun onMenuItemActionCollapse(item: MenuItem): Boolean {
// Called when SearchView is collapsing
// Custom code to run on back pressed
return true
}
override fun onMenuItemActionExpand(item: MenuItem): Boolean {
// Called when SearchView is expanding
// Custom code to run on search view activated
return true
}
})
如何处理在 androidx.appcompat.widget.SearchView
中单击后退箭头
我找到了这个答案:How to handle back arrow event in a SearchView 但它对我不起作用。我从片段设置菜单。
在 MainActivity 中我有这个功能:
override fun onOptionsItemSelected(item: MenuItem): Boolean
{
Log.d("MyTag", "Activity")
return super.onOptionsItemSelected(item)
}
override fun onSupportNavigateUp(): Boolean
{
Log.d("MyTag", "Up")
return navController.navigateUp()
}
在片段中:
searchView.setOnQueryTextListener(
object : SearchView.OnQueryTextListener
{
override fun onQueryTextSubmit(p0: String?): Boolean
{
Log.d("MyTag", "onQueryTextSubmit")
hideKeyboard()
return true
}
override fun onQueryTextChange(p0: String?): Boolean
{
return true
}
}
)
override fun onOptionsItemSelected(item: MenuItem): Boolean
{
Log.d("MyTag", "Fragment")
return super.onOptionsItemSelected(item)
}
None 这些函数在我单击后退箭头时被调用。 Logcat 看起来像这样:
// clicking search button
Activity
Fragment
// clicking Enter on keyboard
onQueryTextSubmit
// clicking back arrow (Nothing)
// When I go to the next fragment and navigate back
Activity
Up
为什么我在SearchView中点击后退箭头没有调用任何事件?我怎样才能做到这一点?
感谢 Rustam Samandarov 这里是 onCreateOptionsMenu
的完整代码:
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)
{
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.menu_toolbar_home, menu)
val menuItem = menu.findItem(R.id.itemSearch)
val searchView = menuItem.actionView as SearchView
searchView.setOnQueryTextListener(
object : SearchView.OnQueryTextListener
{
override fun onQueryTextSubmit(p0: String?): Boolean
{
Log.d("MyTag", "onQueryTextSubmit") // called when enter is pressed
hideKeyboard()
return true
}
override fun onQueryTextChange(p0: String?): Boolean
{
return true // called every time when text is changed
}
}
)
menuItem.setOnActionExpandListener(
object : MenuItem.OnActionExpandListener
{
override fun onMenuItemActionExpand(p0: MenuItem?): Boolean
{
Log.d("MyTag", "onMenuItemActionExpand") // called when user opens SearchView
return true;
}
override fun onMenuItemActionCollapse(p0: MenuItem?): Boolean
{
Log.d("MyTag", "onMenuItemActionCollapse") // called when user click back arrow or navigation back button
return true
}
}
)
}
您好,您应该将扩展侦听器设置为搜索视图
searchMenu.setOnActionExpandListener(
object : MenuItem.OnActionExpandListener {
override fun onMenuItemActionCollapse(item: MenuItem): Boolean {
// Called when SearchView is collapsing
// Custom code to run on back pressed
return true
}
override fun onMenuItemActionExpand(item: MenuItem): Boolean {
// Called when SearchView is expanding
// Custom code to run on search view activated
return true
}
})