我做了一个简单的应用程序,它只会显示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”