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.view.View.OnCreateContextMenuListener;
import android.widget.*;
public class ContextMenuTest extends Activity {
private TextView tv=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv=new TextView(this);
tv.setText("hello,ContextMenuTest");
//注册上下文菜单,长按tv两秒左右可以调出ContextMenu
tv.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){
public void onCreateContextMenu(ContextMenu cm,View view,ContextMenuInfo cmi) {
//super.onCreateContextMenu(cm,view,cmi);
cm.setHeaderIcon(R.drawable.icon);
cm.setHeaderTitle("ContextMenu测试");
cm.add(0, 1, 1, "选项一");
cm.add(0, 2, 1, "选项二");
}
});
setContentView(tv);
}
//上下文菜单事件响应
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1: tv.setText("选项一被按下");break;
case 2: tv.setText("选项二被按下");break;
}
return false;
}
}