Dialog dialog;
private void opendialog() {
dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.popup);
dialog.setTitle(R.string.msettings);
RelativeLayout reply_layout = (RelativeLayout) dialog
.findViewById(R.id.reply_layout);
final RelativeLayout ringtone_layout = (RelativeLayout) dialog
.findViewById(R.id.ringtone_layout);
registerForContextMenu(ringtone_layout);
ringtone_layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openContextMenu(ringtone_layout);
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Edit");
menu.add(0, v.getId(), 1, "Delete");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
System.out.println("Inside onContextItemSelected");
return super.onContextItemSelected(item);
}
在对话框中使用上下文菜单时,永远不会调用onContextItemSelected。我的代码有什么问题吗?提前谢谢..
答案 0 :(得分:7)
注意:由于这个答案似乎得到了一些关注(upvotes),我正在编辑代码片段以反映更简洁的问题答案。
您正尝试在对话框中注册视图项的上下文菜单,但是要从活动中注册。这种方法是错误的。您实际上需要子类化Dialog,然后在那里创建和扩展您的视图,然后覆盖onCreateContextMenu()以执行您的工作并注册上下文菜单的视图。然后,您应该在此处创建该对话框的实例。所以它会是这样的:
public class Mydialog extends Dialog {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup);
dialog.setTitle(R.string.msettings);
RelativeLayout reply_layout = (RelativeLayout) findViewById(R.id.reply_layout);
final RelativeLayout ringtone_layout = (RelativeLayout) findViewById(R.id.ringtone_layout);
registerForContextMenu(ringtone_layout);
ringtone_layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openContextMenu(ringtone_layout);
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Edit");
menu.add(0, v.getId(), 1, "Delete");
}
// You should do the processing for the selected context item here. The
// selected context item gets passed in the MenuItem parameter in
// the following method. In my answer I am force calling the onContextItemSelected()
// method but you are free to do the actual processing here itself
@Override
public boolean onMenuItemSelected(int aFeatureId, MenuItem aMenuItem) {
if (aFeatureId==Window.FEATURE_CONTEXT_MENU)
return onContextItemSelected(aMenuItem);
else
return super.onMenuItemSelected(aFeatureId, aMenuItem);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// Avoid using System.out.println() - instead use Android Logging
return super.onContextItemSelected(item);
}
}
现在,您可以创建此对话框的实例,您的视图将成功注册上下文项。所以你的openDialogMethod()现在看起来像:
private void opendialog() {
dialog = new MyDialog(MainActivity.this);
// the context menu will now be registered
dialog.show();
}
虽然您最初将活动的上下文传递给您正在创建的Dialog,但您无法传递这样的上下文菜单创建侦听器。要做到这一点,你必须按照我上面的说明继承你的对话框。
编辑:在查看Zsolt的答案之后,我发现我忽略了你没有的这行代码。你需要:
ringtone_layout.setOnCreateContextMenuListener(this);
你对强行调用上下文菜单的说法实际上是正确的。您只是调用常规菜单,然后将该ID传递给上下文菜单回调。 if子句通过,因为id来自上下文菜单功能。
经过一些进一步的阅读后,看起来你应该在onMenuItemSelected()而不是onContextItemSelected()上进行菜单处理。所以你现在拥有的是正确的,你不需要强行调用其他方法。只需在onMenuItemSelected()中进行处理。
答案 1 :(得分:4)
此问题已在以下SO线程中解决: onContextItemSelected doesn't get called和onContextItemSelected never called using a Dialog with a ListView。
如果答案都没有帮助你,试试这个:
您在活动及其片段中的return false
方法中onContextItemSelected
。
答案 2 :(得分:1)
public class MusicsetDialog extends Dialog implements OnClickListener {
private RelativeLayout ringtone_layout;
public MusicsetDialog(Context context) {
super(context);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_popup);
setTitle(R.string.msettings);
ringtone_layout = (RelativeLayout) findViewById(R.id.ringtone_layout);
ringtone_layout.setOnClickListener(MusicsetDialog.this);
registerForContextMenu(ringtone_layout);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ringtone_layout:
openContextMenu(ringtone_layout);
break;
default:
break;
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Select The Action");
// groupId, itemId, order,title
menu.add(0, v.getId(), 0, "Edit");
menu.add(0, v.getId(), 1, "Delete");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onMenuItemSelected(int aFeatureId, MenuItem aMenuItem) {
if (aFeatureId==Window.FEATURE_CONTEXT_MENU)
return onContextItemSelected(aMenuItem);
else
return super.onMenuItemSelected(aFeatureId, aMenuItem);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// System.out.println("onContextItemSelected");
Log.d("MusicsetDialog", "onContextItemSelected");
return super.onContextItemSelected(item);
}
}
我为mydialog和扩展Dialog创建了一个子类,并遵循Sunil的过程。但仍未调用onContextItemSelected
。所以在做了一些研究后,我覆盖onMenuItemSelected
并将选定的MenuItem传递给onContextItemSelected
,它运行正常。可能是我强行打电话给onContextItemSelected
。我不知道。我是android的新手,任何解释为什么onContextItemSelected
仍未被自动调用将被赞赏..谢谢..
答案 3 :(得分:1)
试试这个
new AlertDialog.Builder(this)
.setSingleChoiceItems(items, 0, null)
.setPositiveButton(R.string.ok_button_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
// Do something useful withe the position of the selected radio button
}
})
.show();