Andriod常用Menu之一:ContextMenu
来源:互联网 发布:入骨相思知不知 第五期 编辑:程序博客网 时间:2024/05/16 03:38
ContextMenu通过长摁某个View弹出的上下文菜单
1. 创建
android是通过长按某个视图元素来弹出上下文菜单的,通过setOnCreateContextMenuListener(this)来给一个控件设置上下文菜单事件,通常用于ListView和GrideView.
注意:给控件设置监听事件的时候传递this,并不需要像点击事件那样实现抽象方法,因为父类里本身就包含这个方法,只需要重写,进行自己的实现即可.
2.重写创建Contextmenu方法,实现加载自己的布局
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {getMenuInflater().inflate(R.menu.main_context_menu,menu); super.onCreateContextMenu(menu, v, menuInfo);}
getMenuInflater().inflate(R.menu.main_context_menu,menu)给创建的menu加载要显示的布局文件
下面来解释一下onCreateContextMenu的参数:
- ContextMenu menu:要生成的menu对象
- View v:被设置监听事件的对象,也就是触发创建menu的控件对象,比如listview
- ContextMenu.ContextMenuInfo menuInfo :记录了触发控件的位置,id信息.如果使用在listview上要使用id或者position信息必须强转
int position = ((AdapterView.AdapterContextMenuInfo) menuInfo).position;
3.给menu每个item添加点击事件,通过item 的id区分
@Override public boolean onContextItemSelected(MenuItem item) { ContextMenu.ContextMenuInfo menuInfo; AdapterView.AdapterContextMenuInfo adapterContextMenuInfo; switch (item.getItemId()){ case R.id.main_context_menu_delete: menuInfo = item.getMenuInfo(); adapterContextMenuInfo = (AdapterView.AdapterContextMenuInfo) menuInfo; mDatabase.delete("table_pwd","_id=?",new String[]{String.format("%d",adapterContextMenuInfo.id)}); refreshData(); break; case R.id.main_context_menu_edit: adapterContextMenuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); long id = adapterContextMenuInfo.id; Intent intent = new Intent(this, EditActivity.class); intent.putExtra("id",id); startActivity(intent); break; case R.id.main_context_menu_openWeb: long webId = ((AdapterView.AdapterContextMenuInfo) item.getMenuInfo()).position; View childAt = mListview.getChildAt((int) webId); //Long 强转int CharSequence text = ((TextView) ((GridLayout) childAt).getChildAt(4)).getText(); //从列表中获取网址 if (text != null&&!"".equals(text.toString())) { String web = text.toString(); web="http:/"+web;// Toast.makeText(MainActivity.this, web, Toast.LENGTH_SHORT).show(); Intent intent1 = new Intent(); intent1.setAction(Intent.ACTION_VIEW); intent1.setData(Uri.parse(web)); startActivity(intent1); }else { Toast.makeText(MainActivity.this, "该条目不存在网址信息", Toast.LENGTH_SHORT).show(); }// Toast.makeText(MainActivity.this, "id:"+webid, Toast.LENGTH_SHORT).show(); break; } return super.onContextItemSelected(item); }
1 0
- Andriod常用Menu之一:ContextMenu
- Menu菜单之ContextMenu
- Android menu菜单 ContextMenu
- Android系统五种常用的 Menu!OptionMenu/ContextMenu/Actionbar/Contextual aciton mode/popup menu
- Android Menu 之 ContextMenu 详解
- How to use ContextMenu menu.
- menu和ContextMenu的xml写法
- 基础二:ContextMenu,Option Menu和PopupMenu
- Andriod开发历程之一
- AndroidGUI12:ContextMenu的常用技巧
- ContextMenu
- ContextMenu
- ContextMenu
- ContextMenu
- contextmenu
- ContextMenu
- ContextMenu
- ContextMenu
- 坚持#第34天~上了一天课
- Spring Cloud Sleuth使用简介
- android.content.ActivityNotFoundException: No Activity found to handle Intent
- Windows系统下python与NumPy安装方法
- 英语学习——手如柔荑
- Andriod常用Menu之一:ContextMenu
- javascript helloworld 及数组
- >Hibernate 报错:this&nb…
- java的简单ftp上传文件案例
- hibernatesessionfactory
- shell获取时间
- VimTutor中文版
- mac os x下安装 ofbiz
- vncserver的详细配置