我正在尝试访问菜单项,以便我可以应用click事件但我无法执行此操作因为我已经使用给定的代码进行显示并单击菜单项我有一个活动里面有片段我必须访问菜单项在片段和那里我已经在菜单上应用点击功能但我不知道为什么这不会来请建议我给出的代码有什么问题
Menuitem.xml
<?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/action_advance_search_query"
android:enabled="false"
android:title="@string/label_search"
app:showAsAction="always" />
</menu>
活动电话
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.action_advance_search_query -> {
// if (mListener != null) {
// mListener?.onAdvanceSearchQuery()
// }
}
}
return false
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val inflater:MenuInflater = menuInflater
inflater.inflate(R.menu.menu_cs_search_advance, menu);
return true;
}
片段致电:
onCreatView{
setHasOptionsMenu(true)
}
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
inflater?.inflate(R.menu.menu_cs_search_advance, menu)
this.menu = menu
super.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.action_advance_search_query -> {
if (mListener != null) {
mListener?.onAdvanceSearchQuery()
}
}
}
return false
}