package com.example.android_menu_xml; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.ActionMode; import android.view.ActionMode.Callback; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.SubMenu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.PopupMenu; import android.widget.Toast; public class MainActivity extends Activity implements OnItemLongClickListener,OnItemClickListener { private ListView lv; private ArrayAdapter<String> mArrayAdapter; public Context context = MainActivity.this; private SubMenu subMenu; ActionMode mActionMode; private ActionMode.Callback callback = new Callback() { @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { // TODO Auto-generated method stub return false; } @Override public void onDestroyActionMode(ActionMode mode) { // TODO Auto-generated method stub mActionMode = null; } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { // TODO Auto-generated method stub MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.main, menu); return true; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.add: Toast.makeText(context, "onActionItemClicked,add", 1).show(); break; case R.id.edit: Toast.makeText(context, "onActionItemClicked,edit", 1).show(); break; case R.id.del: Toast.makeText(context, "onActionItemClicked,del", 1).show(); break; } return true; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); lv.setAdapter(mArrayAdapter); registerForContextMenu(lv); lv.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub mActionMode = startActionMode(callback); view.setSelected(true); return false; } }); } void init() { lv = (ListView) findViewById(R.id.listView1); mArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, getData()); } public void showpopMenu(View view) { PopupMenu popupMenu = new PopupMenu(context, view); popupMenu.getMenuInflater().inflate(R.menu.menu1, popupMenu.getMenu()); popupMenu.show(); } List<String> getData() { List<String> mlist = new ArrayList<String>(); for (int i = 0; i < 10; i++) { mlist.add("minfan" + i); } return mlist; } @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub AdapterContextMenuInfo info = (AdapterContextMenuInfo) item .getMenuInfo(); String value = mArrayAdapter.getItem(info.position); switch (item.getItemId()) { case R.id.item1: Toast.makeText(context, "item1" + value, Toast.LENGTH_SHORT).show(); break; case R.id.item2: Toast.makeText(context, "item2" + value, Toast.LENGTH_SHORT).show(); break; case R.id.item3: Toast.makeText(context, "item3" + value, Toast.LENGTH_SHORT).show(); break; } return super.onContextItemSelected(item); } @Override public void onCreateContextMenu(android.view.ContextMenu menu, View v, android.view.ContextMenu.ContextMenuInfo menuInfo) { getMenuInflater().inflate(R.menu.menu1, menu); }; @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub getMenuInflater().inflate(R.menu.main, menu);// 填充menu文件夹中的main.xml菜单layout return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) {// 对菜单的子项实现监控 // TODO Auto-generated method stub subMenu = item.getSubMenu(); switch (item.getItemId()) { case R.id.add: Toast.makeText(MainActivity.this, "add", Toast.LENGTH_SHORT).show(); break; case R.id.edit: Toast.makeText(MainActivity.this, "edit", Toast.LENGTH_SHORT) .show(); break; case R.id.del: Toast.makeText(MainActivity.this, "del", Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.addall: Toast.makeText(MainActivity.this, "addall", Toast.LENGTH_SHORT) .show(); break; case R.id.addsomeone: Toast.makeText(MainActivity.this, "addsomeone", Toast.LENGTH_SHORT) .show(); break; } return super.onMenuItemSelected(featureId, item); } @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub switch (view.getId()) { case R.id.addall: Toast.makeText(MainActivity.this, "onItemLongClick,addall", Toast.LENGTH_SHORT) .show(); break; case R.id.addsomeone: Toast.makeText(MainActivity.this, "onItemLongClick,addsomeone", Toast.LENGTH_SHORT) .show(); break; } return true; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub switch (view.getId()) { case R.id.addall: Toast.makeText(MainActivity.this, "onItemClick,addall", Toast.LENGTH_SHORT) .show(); break; case R.id.addsomeone: Toast.makeText(MainActivity.this, "onItemClick,addsomeone", Toast.LENGTH_SHORT) .show(); break; } } }