PopupMenu onMenuItemClick 未被调用
PopupMenu onMenuItemClick not being called
我有一个 class AddPhotoMenu
public class AddPhotoMenu extends PopupMenu implements PopupMenu.OnMenuItemClickListener {
public AddPhotoMenu(Context context, View anchor) {
super(context, anchor);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
Log.d(TAG, "onMenuItemClick: Called");
switch (item.getItemId()) {
case R.id.popup_menu_fragevent_takephoto:
//Inflate a layout
Log.d(TAG, "onMenuItemClick: Take photo");
break;
case R.id.popup_menu_fragevent_selectphotos:
//Inflate a layout
Log.d(TAG, "onMenuItemClick: Select photo");
break;
}
return false;
}
}
我创建了它的一个新实例,但是当我单击菜单项时从未调用方法 onMenuItemClick。
AddPhotoMenu addPhotoMenu = new AddPhotoMenu(this, mAddPhotosButton1);
addPhotoMenu.inflate(R.menu.popup_menu_fragevent_addphotos);
addPhotoMenu.show();
是什么导致了这个问题?
编辑:__________________________________________________
XML 菜单
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/popup_menu_fragevent_takephoto"
android:title="Take a photo"/>
<item
android:id="@+id/popup_menu_fragevent_selectphotos"
android:title="Select photos"/>
</menu>
这样试试
@Override
public boolean onMenuItemClick(MenuItem item) {
Log.d(TAG, "onMenuItemClick: Called");
switch (item.getItemId()) {
case R.id.popup_menu_fragevent_takephoto:
//Inflate a layout
Log.d(TAG, "onMenuItemClick: Take photo");
return true; // return true instead of break
case R.id.popup_menu_fragevent_selectphotos:
//Inflate a layout
Log.d(TAG, "onMenuItemClick: Select photo");
return true; // return true instead of break
}
return false;
}
更新:
您必须将 MenuItemClickListener
设置为弹出菜单,如下所示。
AddPhotoMenu addPhotoMenu = new AddPhotoMenu(this, mAddPhotosButton1);
addPhotoMenu.inflate(R.menu.popup_menu_fragevent_addphotos);
addPhotoMenu.setOnMenuItemClickListener(addPhotoMenu); // add this line
addPhotoMenu.show();
您 PopupMenu.OnMenuItemClickListener 应该在实例化 PopupMen 的 Activity 或 Fragment 中实现。
之后应调用 addPhotoMenu.setOnMenuItemClickListener(this)
,其中 "this" 代表您的片段或 Activity.
我有一个 class AddPhotoMenu
public class AddPhotoMenu extends PopupMenu implements PopupMenu.OnMenuItemClickListener {
public AddPhotoMenu(Context context, View anchor) {
super(context, anchor);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
Log.d(TAG, "onMenuItemClick: Called");
switch (item.getItemId()) {
case R.id.popup_menu_fragevent_takephoto:
//Inflate a layout
Log.d(TAG, "onMenuItemClick: Take photo");
break;
case R.id.popup_menu_fragevent_selectphotos:
//Inflate a layout
Log.d(TAG, "onMenuItemClick: Select photo");
break;
}
return false;
}
}
我创建了它的一个新实例,但是当我单击菜单项时从未调用方法 onMenuItemClick。
AddPhotoMenu addPhotoMenu = new AddPhotoMenu(this, mAddPhotosButton1);
addPhotoMenu.inflate(R.menu.popup_menu_fragevent_addphotos);
addPhotoMenu.show();
是什么导致了这个问题?
编辑:__________________________________________________
XML 菜单
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/popup_menu_fragevent_takephoto"
android:title="Take a photo"/>
<item
android:id="@+id/popup_menu_fragevent_selectphotos"
android:title="Select photos"/>
</menu>
这样试试
@Override
public boolean onMenuItemClick(MenuItem item) {
Log.d(TAG, "onMenuItemClick: Called");
switch (item.getItemId()) {
case R.id.popup_menu_fragevent_takephoto:
//Inflate a layout
Log.d(TAG, "onMenuItemClick: Take photo");
return true; // return true instead of break
case R.id.popup_menu_fragevent_selectphotos:
//Inflate a layout
Log.d(TAG, "onMenuItemClick: Select photo");
return true; // return true instead of break
}
return false;
}
更新:
您必须将 MenuItemClickListener
设置为弹出菜单,如下所示。
AddPhotoMenu addPhotoMenu = new AddPhotoMenu(this, mAddPhotosButton1);
addPhotoMenu.inflate(R.menu.popup_menu_fragevent_addphotos);
addPhotoMenu.setOnMenuItemClickListener(addPhotoMenu); // add this line
addPhotoMenu.show();
您 PopupMenu.OnMenuItemClickListener 应该在实例化 PopupMen 的 Activity 或 Fragment 中实现。
之后应调用 addPhotoMenu.setOnMenuItemClickListener(this)
,其中 "this" 代表您的片段或 Activity.