android 菜单分类:
菜单被分为如下三种,选项菜单(OptionsMenu)、上下文菜单(ContextMenu)和子菜单(SubMenu)
honeycomb 之前版本设备上主要使用 浮动 上下文菜单 ,之后的使用上下文操作栏模式(action mode) 来进行操作

类似:

上下文菜单与上下文操作模式_ide

<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;
}
};

}

上下文菜单与上下文操作模式_ico_02


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);
}