Today, I encountered a bug. I added a menu to the toolbar, but there is no response when I click it. In other words, this button cannot be clicked.
Menu file menu_ toolbar_ announcement_ comment:
<?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/menu_comment"
android:title="@string/comment"
android:visible="true"
app:actionLayout="@layout/menu_provider_number_subscript"
app:showAsAction="always" />
</menu>
code:
mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_comment:
//enable comment
showComment();
break;
}
return true;
}
});
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_toolbar_announcement_comment, menu);
return true;
}
The setonmenuitemclicklistener method in the code doesn’t work.
I think the reason may be that I use custom layout in menu
app: actionlayout = @ layout/menu_ provider_ number_ subscript"
At this time, you need to write the click event of the menu in oncreateoptionsmenu
The details are as follows:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_toolbar_announcement_comment, menu);
MenuItem item = menu.findItem(R.id.menu_comment);
item.getActionView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//enable comment
showComment();
}
});
return true;
}
Read More:
- Win10, add sublime text in the right mouse button menu to open
- A the tag is grayed out and cannot be clicked
- [Android] button uses the custom drawable file to set the background, and runs “error inflating class button”
- Android listview entry button click state chaos solution
- Content rendering error: a solution to the problem of zero Download document cannot be opened
- Two methods of fixing WPF menu bar after scrolling to the top
- “Input button submit is not setup” solution
- Android Studio:Unresolved reference: toolbar
- Solution to the problem that some special characters in finereport cannot be displayed or displayed as question mark
- How to use the menu of elementui unique opened sidebar
- MacOS: How to Fix Intellij-IDEA main menu disappears Bug
- Problem solving: PDF cannot be opened, and acrobat failed to connect to a DDE server appears
- Python combined with Matplotlib to add button
- Wamp Apache can’t run error report could not execute menu item
- Solutions to the problem that CodeBlocks cannot be used
- Sublime text add to right mouse button function
- Solution to the problem of keyword not supported data source
- Solutions to the problem of “Autowired cannot be resolved to a type” in eclipse Maven project
- Click the button to jump to a new window
- CentOS solves the problem of modulenotfounderror when salt calls python3 script remotely, and solves the problem that PIP3 module cannot be shared by different users