1.ActionBar中的菜单:http://www.tuicool.com/articles/VrUBRf
2.ContextMenu(上下文菜单)
- 首先在res/menu/中创建一个XML文件,以定义菜单,我们将其命名为options
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <item android:id="@+id/add_item" android:title="Add Item" android:icon="@drawable/ic_launcher" android:showAsAction="ifRoom" tools:ignore="AppCompatResource" /> <item android:id="@+id/remove_item" android:title="remove Item" android:icon="@drawable/ic_launcher" android:showAsAction="always" tools:ignore="AppCompatResource" /> <item android:id="@+id/menu_edit" android:title="edit Item" android:icon="@drawable/ic_launcher" android:showAsAction="never" tools:ignore="AppCompatResource" /> </menu>
- 然后再Activity中用onCreatContextMenu()和onContextItemSelected()方法填充菜单,处理用户的选择
-
1 @Override 2 public boolean onContextItemSelected(MenuItem item) { 3 switch(item.getItemId()){ 4 case R.id.add_item: 5 //执行添加动作 6 break; 7 case R.id.remove_item: 8 //执行删除动作 9 break; 10 case R.id.menu_edit: 11 break; 12 default: 13 return super.onContextItemSelected(item); 14 } 15 return true; 16 } 17 18 19 @Override 20 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 21 menu.setHeaderTitle("Choose an option"); 22 getMenuInflater().inflate(R.menu.options,menu); 23 super.onCreateContextMenu(menu, v, menuInfo); 24 25 }
- 默认是用户长按item时会触发菜单项,进而弹出选项框
- 使用时要使用registerForContextMenu(View view)来注册菜单,即将ContextMenu绑定到某个view中
- 示例:
1 package com.example.qjm3662.service_learn; 2 3 import android.content.ComponentName; 4 import android.content.Intent; 5 import android.content.ServiceConnection; 6 import android.os.IBinder; 7 import android.support.v7.app.AppCompatActivity; 8 import android.os.Bundle; 9 import android.view.ContextMenu; 10 import android.view.Menu; 11 import android.view.MenuItem; 12 import android.view.View; 13 import android.view.Window; 14 import android.widget.ArrayAdapter; 15 import android.widget.ListView; 16 import android.widget.TimePicker; 17 import android.widget.Toast; 18 19 public class MainActivity extends AppCompatActivity { 20 21 private static final String[] ITEMS = {"Lost","Time","Will","Never","Be","Find","again!"}; 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 ListView list = new ListView(this); 26 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ITEMS); 27 list.setAdapter(adapter); 28 registerForContextMenu(list); 29 setContentView(list); 30 } 31 32 33 34 35 @Override 36 public boolean onContextItemSelected(MenuItem item) { 37 switch(item.getItemId()){ 38 case R.id.add_item: 39 //执行添加动作 40 break; 41 case R.id.remove_item: 42 //执行删除动作 43 break; 44 case R.id.menu_edit: 45 break; 46 default: 47 return super.onContextItemSelected(item); 48 } 49 return true; 50 } 51 52 53 @Override 54 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 55 menu.setHeaderTitle("Choose an option"); 56 getMenuInflater().inflate(R.menu.options,menu); 57 super.onCreateContextMenu(menu, v, menuInfo); 58 59 } 60 61 62 63 }
3.ActionMode
激活ActionMode后会在系统的ActionBar上出现一个自定义的菜单,覆盖在原ActionBar上,并出现一个额外的可以退出ActionMode的选项。还允许同时选择多个条目来执行同一个动作。
1 package com.example.qjm3662.service_learn; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.ActionMode; 6 import android.view.Menu; 7 import android.view.MenuItem; 8 import android.view.View; 9 import android.widget.AbsListView; 10 import android.widget.AdapterView; 11 import android.widget.ArrayAdapter; 12 import android.widget.ListView; 13 14 public class MainActivity extends AppCompatActivity implements AbsListView.MultiChoiceModeListener{ 15 16 ListView list; 17 private static final String[] ITEMS = {"Lost","Time","Will","Never","Be","Find","again!"}; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 list = new ListView(this); 22 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ITEMS); 23 list.setAdapter(adapter); 24 list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 25 list.setMultiChoiceModeListener(this); 26 setContentView(list); 27 } 28 29 30 @Override 31 //当子控件的选择状态发生改变时调用该方法 32 public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { 33 //获得list中被选中的item数目 34 int count = list.getCheckedItemCount(); 35 mode.setTitle(String.format("%d Selected", count)); 36 } 37 38 @Override 39 public boolean onCreateActionMode(ActionMode mode, Menu menu) { 40 getMenuInflater().inflate(R.menu.options,menu); 41 return true; 42 } 43 44 @Override 45 public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 46 //如果ActionMode一直是无效,可以在这里做些工作来更新菜单 47 return true; 48 } 49 50 @Override 51 public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 52 //通过条目的Id得到用户的选择动作 53 switch (item.getItemId()){ 54 case R.id.add_item: 55 //执行添加动作 56 break; 57 case R.id.remove_item: 58 //执行删除动作 59 break; 60 case R.id.menu_edit: 61 break; 62 default: 63 return false; 64 } 65 return true; 66 } 67 68 @Override 69 public void onDestroyActionMode(ActionMode mode) { 70 //退出ActionMode时会调用该方法 71 } 72 }