onTextContextMenuItem is not called on custom menu items
I'm trying to add some custom menu items to some EditText-instances. They appear on in the menu, but when I click the buttons the onTextContentMenuItem-method is not called. The EditText-instances are in a ListView if that matters.
Any advice on the matter?
Relevant code:
class DocumentFragment extends EditText {
public DocumentFragment(Context context) {
super(context);
}
@Override
public void onCreateC开发者_JS百科ontextMenu(ContextMenu menu) {
menu.add(Menu.CATEGORY_ALTERNATIVE, CONVERT_TO_H1, Menu.NONE, "Convert to H1");
menu.add(Menu.CATEGORY_ALTERNATIVE, CONVERT_TO_P, Menu.NONE, "Convert to P");
super.onCreateContextMenu(menu);
}
@Override
public boolean onTextContextMenuItem(int id) {
Log.i("ID", String.valueOf(id)); // The id of CONVERT_TO_{H1,P} never appears.
return super.onTextContextMenuItem(id);
}
}
It's very easy... after look for in the EditText
and TextView
sources...
First, you must implement the interface MenuItem.OnMenuItemClickListener
in your EditText
class.
Second, you must implement the onMenuItemClick
for the interface, like so
@Override
public boolean onMenuItemClick(MenuItem item)
{
return onTextContextMenuItem( item.getItemId() );
}
Last, add to each menu item the listener,
menu.add( Menu.NONE, MyMenu, Menu.CATEGORY_SECONDARY, "Menu text" ) ).setOnMenuItemClickListener( this );
This will be called in Activity.onContextItemSelected(MenuItem item). You have to handle it from there.
精彩评论