Android 学习笔记(六):ContextMenu上下文菜单
ContextMenu上下文菜单,和电脑上单击鼠标右键出现的快捷菜单等同,即安卓中长按某项(2秒左右)出现的快捷菜单,
ContextMenu上下文菜单创建方法:
1.重写Activity的onCreateContextMenu()方法,调用Menu的add方法添加菜单项
2.重写onContextItemSelected()方法,响应菜单单击事件
3.调用registerForContexMenu()注册上下文菜单
main.xml
[html] <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>TestActivity.java
[java] public class TestActivity extends Activity {
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView=(ListView)findViewById(R.id.listView);
//为listView注册上下文菜单ContextMenu
//方法1
this.registerForContextMenu(listView);
//方法2
//listView.setOnCreateContextMenuListener(this);
String [] s={"特凡·萨尔瓦托 Stefan Salvatore",
"艾琳娜·吉尔伯特 Elena Gilbert",
"达蒙·萨尔瓦托 Damon Salvatore",
"杰里米·吉尔伯特 Jeremy Gilbert",
"卡罗琳·福布斯 Caroline Forbes"};
ArrayAdapter aa=new ArrayAdapter<String>(this,android.R.layout.易做图_list_item_1,s);
listView.setAdapter(aa);
}
//创建上下文菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo mi) {
menu.setHeaderTitle("你猜啊~ 0,0");
//配置上下文菜单选项
menu.add(0, Menu.FIRST +1, 1, "海贼王");
menu.add(0, Menu.FIRST +2, 2, "暮光之城");
menu.add(0, Menu.FIRST +3, 3, "吸血鬼日记");
menu.add(0, Menu.FIRST +4, 4, "齐天大圣");
//super.onCreateContextMenu(menu, v, mi);
}
//响应上下文菜单的点击事件
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getItemId()==Menu.FIRST + 3){
Toast.makeText(this, "ORZ,你聪明易做图啦!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "这有个毛联系!", Toast.LENGTH_SHORT).show();
}
return super.onContextItemSelected(item);
}
}
public class TestActivity extends Activity {
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView=(ListView)findViewById(R.id.listView);
//为listView注册上下文菜单ContextMenu
//方法1
this.registerForContextMenu(listView);
//方法2
//listView.setOnCreateContextMenuListener(this);
String [] s={"特凡·萨尔瓦托 Stefan Salvatore",
"艾琳娜·吉尔伯特 Elena Gilbert",
"达蒙·萨尔瓦托 Damon Salvatore",
"杰里米·吉尔伯特 Jeremy Gilbert",
"卡罗琳·福布斯 Caroline Forbes"};
ArrayAdapter aa=new ArrayAdapter<String>(this,android.R.layout.易做图_list_item_1,s);
listView.setAdapter(aa);
}
//创建上下文菜单 www.zzzyk.com
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo mi) {
menu.setHeaderTitle("你猜啊~ 0,0");
//配置上下文菜单选项
menu.add(0, Menu.FIRST +1, 1, "海贼王");
menu.add(0, Menu.FIRST +2, 2, "暮光之城");
menu.add(0, Menu.FIRST +3, 3, "吸血鬼日记");
&n
补充:移动开发 , Android ,