编程小白一枚,如题,如何才能做到,我现在已经在app中添加了浮动的导航菜单,我希望在点击导航菜单的时候启动一个新的activity,但是下面的代码完成不了,请大神看一下
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_view);
dataSource = new DBDataSource(this);
dataSource.open();
values = dataSource.getAllMahasiswa();
ArrayAdapter<mahasiswa> adapter = new ArrayAdapter<mahasiswa> (this, android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
listView=(ListView) findViewById(android.R.id.list);
registerForContextMenu(listView);
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, 1000, 0, "Edit");
menu.add(0, 1001, 0, "Hapus");
}
public boolean onContextMenuItemSelected(MenuItem item){
switch (item.getItemId()) {
case 1000:
Intent i = new Intent(ActivityView.this, Tes.class);
startActivity(i);
break;
case 1001:
//not yet
break;
}
return super.onContextItemSelected(item);
}</mahasiswa></mahasiswa>
处理方法
在android开发中共有两种方式完成,使用下面的2代码 代码一 @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.pick_camera:
break; } return super.onContextItemSelected(item); } 代码二 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.image_options, menu); super.onCreateContextMenu(menu, v, menuInfo); }
在 菜单资源路径创建image_options.xml create image_options.xml in menu folder of resource. <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android " >
<item android:id="@+id/pick_camera" android:title="Use Camera"> </item>
</menu>
原文地址:http://www.itmmd.com/201411/149.html 该文章由 萌萌的IT人 整理发布,转载须标明出处。