上下文菜单与上下文操作模式
2015-09-08 14:21
465 查看
android 菜单分类:
菜单被分为如下三种,选项菜单(OptionsMenu)、上下文菜单(ContextMenu)和子菜单(SubMenu)
honeycomb 之前版本设备上主要使用 浮动 上下文菜单 ,之后的使用上下文操作栏模式(action mode) 来进行操作
Context menu
1.创建
2.option Context 区别
option context 通过手机自带的meum 进行调用触发,而 ContextMeum 通过 指定的方式进行触发
参考:/article/1361178.html
android 权威指南 18 章节
菜单被分为如下三种,选项菜单(OptionsMenu)、上下文菜单(ContextMenu)和子菜单(SubMenu)
honeycomb 之前版本设备上主要使用 浮动 上下文菜单 ,之后的使用上下文操作栏模式(action mode) 来进行操作
类似:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/action_settings" android:icon="@android:drawable/ic_delete" android:orderInCategory="100" android:showAsAction="ifRoom|withText" android:title="@string/app_name" /> <item android:id="@+id/action_settings1" android:icon="@android:drawable/ic_menu_add" android:orderInCategory="100" android:showAsAction="ifRoom|withText" android:title="@string/app_name" /> <item android:id="@+id/action_settings2" android:icon="@android:drawable/ic_btn_speak_now" android:orderInCategory="100" android:showAsAction="ifRoom|withText" android:title="@string/say" /> </menu>
MainActivity 代码片段 package com.wc.contextmenu; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.ActionMode; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends FragmentActivity { private ActionMode actionMode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // CrimeListFragment crimeListFragment = new CrimeListFragment(); // this.getSupportFragmentManager().beginTransaction().replace(R.id.fragment, crimeListFragment).commit(); Button button = (Button) findViewById(R.id.button); button.setOnLongClickListener(new View.OnLongClickListener() { // Called when the user long-clicks on someView public boolean onLongClick(View view) { // if (actionMode != null) { // return false; // } // Start the CAB using the ActionMode.Callback defined above actionMode = MainActivity.this.startActionMode(mCallback); view.setSelected(true); return true; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { //getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case R.id.action_settings: break; default: return super.onOptionsItemSelected(item); } return super.onOptionsItemSelected(item); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { // MainActivity.this.getMenuInflater().inflate(R.menu.menu_main, menu); } private ActionMode.Callback mCallback = new ActionMode.Callback() { @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } @Override public void onDestroyActionMode(ActionMode mode) { // TODO Auto-generated method stub } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.menu_main, menu); return true; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { boolean ret = false; if (item.getItemId() == R.id.action_settings) { mode.finish(); ret = true; } return ret; } }; }
Context menu
1.创建
2.option Context 区别
option context 通过手机自带的meum 进行调用触发,而 ContextMeum 通过 指定的方式进行触发
在这里进行创建 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); } 监听点击事件 @Override public boolean onContextItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case R.id.action_settings: //获取点击的位置。得到对象 AdapterView.AdapterContextMenuInfo adapterContextMenuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); int position = adapterContextMenuInfo.position; ArrayAdapter arrayAdapter = (ArrayAdapter) getListAdapter(); String showParamter = (String) arrayAdapter.getItem(position); list1.remove(showParamter); ((ArrayAdapter) getListAdapter()).notifyDataSetChanged(); break; case R.id.action_settings1: Toast.makeText(getActivity(), "click 上下文菜单 --在这里可以进行删除操作", 400).show(); break; default: return super.onOptionsItemSelected(item); } return super.onContextItemSelected(item); }
参考:/article/1361178.html
android 权威指南 18 章节
相关文章推荐
- Vysor 在电脑上控制android手机屏幕 镜像
- sql server date,datetime,datetime2 和 time 简单介绍
- display:block; 块级元素。<a>,<span>标签设置宽度和高度
- 前台页面对象不支持“open”属性或方法
- 实现跨域的N种方法
- 圆角文本框
- 圆角文本框
- 安装hive后使用mysql作为数据库无法正常启动问题
- mvn 常用的命令
- Unity 3D 第三更
- Android开发之使用viewpager实现图片轮播(二)
- 实训第一次成果汇报
- 知识点
- 转身,遇到秋
- SpringMV 4000 C返回json数据的三种方式
- sh脚本异常
- C++ Vector用法深入剖析
- tcp窗口滑动以及拥塞控制
- 解决airserver在Windows下安装失败的问题
- [转]DllMain中不当操作导致死锁问题的分析——DllMain中要谨慎写代码(完结篇)