我创建了一个简单的应用程序,只显示一个AlertDialog,列表中有四个项目。我注册了一个上下文菜单。当我长按其中一个项目时,我会看到上下文菜单。然后我从上下文菜单中选择一个项目,但onContextItemSelected永远不会被调用。有帮助吗?感谢。
test.java:
package com.cerulean.tech.creations.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
public class test extends Activity {
private String[] files;
AlertDialog alert;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
files = new String[4];
}
public void selectScheme(View v) {
files[0] = "<New Scheme>";
files[1] = "test1";
files[2] = "test2";
files[3] = "test3";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setItems(files, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
}});
alert = builder.create();
alert.show();
registerForContextMenu(alert.getListView());
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Delete");
menu.add(0, v.getId(), 0, "Cancel");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return false;
}
}
在main.xml中,我只是一个用android定义的按钮:onClick =“selectScheme”
答案 0 :(得分:9)
这一行之后:
registerForContextMenu(alert.getListView());
输入:
alert.getListView().setOnCreateContextMenuListener(this);
而不是onContextItemSelected(MenuItem item)函数使用这个:
@Override
public boolean onMenuItemSelected(int featureId, MenuItem menuItem) {
答案 1 :(得分:4)
始终执行以下功能。
public boolean onMenuItemSelected(int featureId, MenuItem menuItem)
但您可以使用以下标志区分上下文菜单和选项菜单:
if (featureId == Window.FEATURE_CONTEXT_MENU)
{
//Do something
}
else if (featureId == Window.FEATURE_OPTIONS_PANEL)
{
//Do something else
}
答案 2 :(得分:1)
添加
@Override
public boolean onMenuItemSelected(int aFeatureId, MenuItem aMenuItem) {
if (aFeatureId==Window.FEATURE_CONTEXT_MENU)
return onContextItemSelected(aMenuItem);
else
return super.onMenuItemSelected(aFeatureId, aMenuItem);
}
答案 3 :(得分:0)
我希望以Miki的答案为基础。
我只用了
registerForContextMenu(this.getListView());
和
this.getListView().setOnCreateContextMenuListener(this);
在我的父ListActivity类中。
我将代码从 onContextItemSelected(MenuItem item)方法移至 onMenuItemSelected 方法。
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
try {
if (item.getItemId() == R.id.new_entry_menu_option) {
...
}
...
if (item.getItemId() == R.id.quit) {
/* perform cleanup */
...
return true;
} else if (item.getItemId() == R.id.delete_entry_context_menu_option) {
displayConfirmRequest(DELETE_CONFIRMATION_MESSAGE, item);
return true;
} else if (item.getItemId() == R.id.2ND_CONTEXT_OPTION) {
//code for 2nd option
return true;
} else if (item.getItemId() == R.id.3RD_CONTEXT_OPTION) {
//code for 3RD option
return true;
} else {
return super.onMenuItemSelected(featureId, item);
}// end if/else if/else
}// end try
catch (Exception error) {
//error handler code
return false;
}// end try/catch
}// end onMenuItemSelected
并确保在您的子类中,如果覆盖 super.ListActivity 类的子类中的onMenuItemSelected ,则包含以下代码(如果您需要)要在super.class中处理的contextmenu选项。
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (<condition>) {
...
} else {
return super.onMenuItemSelected(featureId, item);
}
}// end onMenuItemSelected
答案 4 :(得分:-1)
此问题的真正原因是您覆盖onContextItemSelected
而未调用super.onContextItemSelected
。将您的onContextItemSelected
方法更改为:
@Override
public boolean onContextItemSelected(MenuItem item) {
if (super.onContextItemSelected(MenuItem item))
return true;
return false;
}