2013年8月26日星期一

Android

 

Introduction

 

menu Menu explain this blog use. Menu in windows applications using a very broad, almost all windows applications have menus, Android also added menu support. From the official documentation has learned from Android3.0 (API level 11) begins, Android devices are no longer required to provide a dedicated menu button and instead recommended ActionBar. So now on the market a lot of new devices use three virtual buttons, and no additional menu buttons, but the buttons are also used in some places can learn from.

 

because the version of Android development, support for various versions of the menu there is a big difference, but Android3.0 was a watershed, can be divided into the following three categories:

 
      
  • OptionMenu and ActionBar: a collection of some operations, if the development of the platform Android3.0 above, it is recommended to use ActionBar, if the development of the platform Android2.3 or below, or you can use OptionMenu's.
  •   
  • ContextMenu and ActionMode: ContextMenu is a floating window to show the form of a list of options, ActionMode is displayed on the screen at the top of the action bar, which allows the user to select multiple options, ActionMode only after the Android3.0 support.
  •   
  • Pupop Menu: PopupMenu is fixed on the View menu on the modal to the pop-up display, only after the Android3.0 support.
  •  
 

 

a menu defined in XML

 

Android provides a standard resource file in XML format to define the menu items and menus for all types are supported, it is recommended to use an XML resource file to define the menu, and then after it Inflater to Activity or Fragment, rather than in the Activity Using the code declaration.

 

the menu XML resource file, you need to create the / res / menu / directory, and contains about several elements:

 
      
  • : Define a Menu, a root menu resource files, which can contain one or more and elements.
  •   
  • : Create a MenuItem, represents one of the options menu.
  •   
  • : group items on the menu, you can operate as a group in the form of a menu item.
  •  
 

elements in addition to conventional id, icon, title attribute support, there is an important attribute: android: showAsAction, this property is from compatibility, describes the high version of Android, the menu item how and when added to the ActionBar.

 

is grouped menu, the menu display is grouped and there is no difference, the only difference is that it can operate for the menu group, such menu items for classification, the operation is more convenient to provide the following operations :

 
      
  • Menu.setGroupCheckable (): whether the menu within the menu group are optional.
  •   
  • Menu.setGroupVisible (): whether to hide menu group all the menus.
  •   
  • Menu.setGroupEnabled (): menu group menu is useful.
  •  
 If the menu item needs

radio or multiple choice, you can use android: checkableBehavior property is set, it can set up a single or group, this attribute accepts three parameters: single, radio; all, multiple choice, none, no Checked option default.

 

When created an XML menu resource file, you can use MenuInflater.inflate () method to populate the menu resource, makes XML resources into a programmable object.

 

 

OptionMenu

 

OptionMenu, the Options menu, you must have the menu button until the device can be triggered. Because the screen limitations, can only show six menu items, if you define a menu item exceeds six, the other menu items will be hidden, and the sixth menu will display the "More" menu, click to expand more . Although no longer recommended for use after the Android3.0 options menu, but if you use the device after Android3.0, Options menu item will be transferred to the ActionBar default, this can be android: showAsAction attribute.

 

Use OptionMenu need to override the Activity or Fragment onCreateOptionsMenu ( Menu ) method, This method is declared in an options menu. Menu exists to provide operation, so Activity and Fragment also provides a onOptionsItemSelected ( MenuItem ) method in response to the Options menu, select the response time. OptionMenu is the operation of a Menu object and MenuItem objects.

 

following section through two Demo to demonstrate the use of the Options menu, respectively, using code declares menus and XML resource file declares the way the menu instructions.

 

declaration code:

 
  
 1 package com.example.menudemo; 
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.view.Menu;
7 import android.view.MenuItem;
8 import android.view.SubMenu;
9 import android.widget.Toast;
10
11 public class OptionMenu1Activitty extends Activity {
12
13 @Override
14 protected void onCreate(Bundle savedInstanceState) {
15 // TODO Auto-generated method stub
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.activity_optionmenu1);
18 }
19
20 @Override
21 public boolean onCreateOptionsMenu(Menu menu) {
22
23 //直接Add菜单选项到Menu中
24 menu.add(1000, 100, 0, "System menu");
25 //获取添加的菜单选项,然后设置其图标
26 MenuItem menuItem2=menu.add(1000, 101, 1, "User menu");
27 menuItem2.setIcon(R.drawable.ic_launcher);
28 //获取添加的菜单选项,增加一个Intent,点击后转向IntentActivity
29 MenuItem menuItem3=menu.add(1000, 102, 2, "Intent menu");
30 menuItem3.setIcon(R.drawable.ic_launcher);
31 Intent intent=new Intent(OptionMenu1Activitty.this, IntentActivity.class);
32 menuItem3.setIntent(intent);
33
34 //添加一个SubMenu,点击后弹出一个子菜单对话框
35 SubMenu submenu=menu.addSubMenu(1000, 103, 3, "Sub menus");
36 submenu.add(1000, 104, 4, "Sub ment1");
37 submenu.add(1000, 105, 4, "Sub ment2");
38 submenu.add(1000, 106, 4, "Sub ment3");
39 return true;
40 }
41
42 @Override
43 public boolean onOptionsItemSelected(MenuItem item) {
44 boolean flag;
45 switch (item.getItemId()) {
46 case 100:
47 Toast.makeText(OptionMenu1Activitty.this, "selected System menu", Toast.LENGTH_SHORT).show();
48 flag=true;
49 break;
50 case 101:
51 Toast.makeText(OptionMenu1Activitty.this, "selected User menu", Toast.LENGTH_SHORT).show();
52 flag=true;
53 break;
54 case 104:
55 Toast.makeText(OptionMenu1Activitty.this, "selected Sub menu1", Toast.LENGTH_SHORT).show();
56 flag=true;
57 default:
58 flag=super.onOptionsItemSelected(item);
59 break;
60 }
61 return flag;
62 }
63
64 }
 
 

 

achieve results, Android2.3:

 

 

using XML resource file defines the options menu, XML resource file:

 
  
 1 <?xml version="1.0" encoding="utf-8"?> 
2 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
3
4 <item
5 android:id="@+id/item1"
6 android:showAsAction="never"
7 android:title="System menu">
8 </item>
9 <item
10 android:id="@+id/item2"
11 android:showAsAction="never"
12 android:title="User menu"
13 android:icon="@drawable/ic_launcher">
14 </item>
15 <item
16 android:id="@+id/item3"
17 android:showAsAction="never"
18 android:title="Intent menu"
19 android:icon="@drawable/ic_launcher">
20 </item>
21 <group android:id="@+id/group_file" >
22 <item android:id="@+id/menu_save"
23 android:title="menu group save" />
24 <item android:id="@+id/menu_delete"
25 android:title="menu group delete" />
26 </group>
27 <item android:id="@+id/file"
28 android:title="Sub menus" >
29 <!-- "file" submenu -->
30 <menu>
31 <item android:id="@+id/sub_menu1"
32 android:title="Sub menu1" />
33 <item android:id="@+id/sub_menu21"
34 android:title="Sub menu2" />
35 <item android:id="@+id/sub_menu3"
36 android:title="Sub menu3" />
37 </menu>
38 </item>
39 </menu>
 
 

Java code:

 
  
 1 package com.example.menudemo; 
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.view.Menu;
7 import android.view.MenuItem;
8 import android.widget.TextView;
9 import android.widget.Toast;
10
11 public class OptionMenu2Activitty extends Activity {
12 private TextView tv;
13 @Override
14 protected void onCreate(Bundle savedInstanceState) {
15 // TODO Auto-generated method stub
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.activity_optionmenu1);
18 tv=(TextView)findViewById(R.id.tvOptionMenu1);
19 tv.setText("加载XML资源填充Menu");
20 }
21
22 @Override
23 public boolean onCreateOptionsMenu(Menu menu) {
24 // 使用布局文件加载菜单
25 getMenuInflater().inflate(R.menu.optionmenu2, menu);
26 return super.onCreateOptionsMenu(menu);
27 }
28
29 @Override
30 public boolean onOptionsItemSelected(MenuItem item) {
31
32 switch (item.getItemId()) {
33 case R.id.item1:
34 Toast.makeText(OptionMenu2Activitty.this, "selected System menu", Toast.LENGTH_SHORT).show();
35 return true;
36 case R.id.item2:
37 Toast.makeText(OptionMenu2Activitty.this, "selected User menu", Toast.LENGTH_SHORT).show();
38 return true;
39 case R.id.item3:
40 Intent intent=new Intent(OptionMenu2Activitty.this, IntentActivity.class);
41 startActivity(intent);
42 return true;
43 case R.id.menu_save:
44 Toast.makeText(OptionMenu2Activitty.this, "file save", Toast.LENGTH_SHORT).show();
45 return true;
46 case R.id.sub_menu1:
47 Toast.makeText(OptionMenu2Activitty.this, "Selected sub_menu1", Toast.LENGTH_SHORT).show();
48 return true;
49 default:
50 return super.onOptionsItemSelected(item);
51 }
52
53
54 }
55
56 }
 
 

effects and use Java code to declare the same menu, there will no longer show up.

 

 

ContextMenu

 

ContextMenu , the context menu provides the components registered in the View menu operation, it is to a floating window displays (similar dialog box), when the user is registered with a long press context menu view, trigger the context menu is displayed. Usually used for ListView or GridView view other collections.

 

using the context menu steps:

 
      
  1. Use Activity.registerForContextMenu (View) method for the specified View registered context menu.
  2.   
  3. override the Activity or Fragment onCreateContextMenu () method, when a view is registered receives long press event, the system calls onCreateContextMenu () method, in this method declaration context menu.
  4.   
  5. achieve onContextItemSelected () method in response to a menu like the check.
  6.  
 

example, the menu XML resource file code:

 
  
 1 <menu xmlns:android="http://schemas.android.com/apk/res/android" > 
2
3 <item
4 android:id="@+id/context_copy"
5 android:orderInCategory="100"
6 android:showAsAction="never"
7 android:title="Copy"/>
8 <item
9 android:id="@+id/context_edit"
10 android:orderInCategory="100"
11 android:showAsAction="never"
12 android:title="Edit"/>
13 <item
14 android:id="@+id/context_delete"
15 android:orderInCategory="100"
16 android:showAsAction="never"
17 android:title="Delete"/>
18 </menu>
 
 

Java code:

 
  
 1 package com.example.menudemo; 
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.app.Activity;
7 import android.os.Bundle;
8 import android.view.ContextMenu;
9 import android.view.MenuInflater;
10 import android.view.MenuItem;
11 import android.view.View;
12 import android.view.ContextMenu.ContextMenuInfo;
13 import android.widget.AdapterView.AdapterContextMenuInfo;
14 import android.widget.ArrayAdapter;
15 import android.widget.ListView;
16 import android.widget.Toast;
17
18 public class ContextMenu1 extends Activity {
19 private ListView listview;
20 private List<String> dataList;
21 @Override
22 protected void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);
24 setContentView(R.layout.activity_contextmenu1);
25 listview=(ListView)findViewById(R.id.listView1);
26 dataList=getData();
27 ArrayAdapter<String> adapter=new ArrayAdapter<String>(ContextMenu1.this,android.R.layout.simple_list_item_1, dataList);
28 listview.setAdapter(adapter);
29 //为ListView注册上下文菜单
30 registerForContextMenu(listview);
31 }
32
33 @Override
34 public void onCreateContextMenu(ContextMenu menu, View v,
35 ContextMenuInfo menuInfo) {
36 super.onCreateContextMenu(menu, v, menuInfo);
37 //填充一个XML菜单文件
38 MenuInflater inflater = getMenuInflater();
39 inflater.inflate(R.menu.contextmenu, menu);
40 }
41
42 @Override
43 public boolean onContextItemSelected(MenuItem item) {
44 //获取上下文菜单绑定的AdapterView的额外信息
45 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
46 switch (item.getItemId()) {
47 case R.id.context_copy:
48 Toast.makeText(ContextMenu1.this, "copy "+dataList.get(info.position), Toast.LENGTH_SHORT).show();
49 return true;
50 case R.id.context_delete:
51 Toast.makeText(ContextMenu1.this, "delete "+dataList.get(info.position), Toast.LENGTH_SHORT).show();
52 return true;
53 case R.id.context_edit:
54 Toast.makeText(ContextMenu1.this, "edit " +dataList.get(info.position), Toast.LENGTH_SHORT).show();
55 return true;
56 default:
57 return super.onContextItemSelected(item);
58 }
59 }
60 //ListView数据
61 public List<String> getData()
62 {
63 List<String> data=new ArrayList<String>();
64 for(int i=0;i<8;i++)
65 {
66 data.add("item"+i);
67 }
68 return data;
69 }
70 }
 
 

results show, Android4.0:

 

 

 

ActionMode

 

ActionMode , is an implementation of the system user interaction when a user uses ActionMode, select an option, a contextual action bar will appear at the top of the screen, showing the user can select the items on the current operation options. This state can be entered through the back button or call finish () to exit. ActionMode after the Android3.0 support, so after application in the development of 3.0 is recommended ActionMode, instead of ContextMenu.

 

Use ActionMode steps:

 
      
  1. achieve ActionMode.Callback interface. In its callback method, you can set the operation in the context of the action bar.
  2.   
  3. need to display context-sensitive action bar when calling startActionMode (ActionMode.Callback).
  4.  
 

ActionMode.Callback is ActionMode define an internal interface that need to achieve The following four methods:

 
      
  • boolean onCreateActionMode (ActionMode mode, Menu menu): the first is created when called.
  •   
  • boolean onPrepareActionMode (ActionMode mode, Menu menu): refresh is called when the menu list, you can generally use false.
  •   
  • boolean onActionItemClicked (ActionMode mode, MenuItem item): menu item is selected when they were called.
  •   
  • void onDestroyActionMode (ActionMode mode): Exit or destroyed when they were called.
  •  
 

Example:

 
  
  1 package com.example.menudemo; 
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.annotation.SuppressLint;
7 import android.app.Activity;
8 import android.os.Bundle;
9 import android.view.ActionMode;
10 import android.view.ActionMode.Callback;
11 import android.view.Menu;
12 import android.view.MenuInflater;
13 import android.view.MenuItem;
14 import android.view.View;
15 import android.widget.AdapterView;
16 import android.widget.AdapterView.AdapterContextMenuInfo;
17 import android.widget.AdapterView.OnItemLongClickListener;
18 import android.widget.ArrayAdapter;
19 import android.widget.ListView;
20 import android.widget.Toast;
21
22 public class ActionModeMenu1 extends Activity {
23 private ListView listview;
24 private List<String> dataList;
25 private ActionMode mActionMode;
26 @Override
27 protected void onCreate(Bundle savedInstanceState) {
28 // TODO Auto-generated method stub
29 super.onCreate(savedInstanceState);
30 setContentView(R.layout.activity_contextmenu1);
31 listview=(ListView)findViewById(R.id.listView1);
32 dataList=getData();
33 ArrayAdapter<String> adapter=new ArrayAdapter<String>(ActionModeMenu1.this,android.R.layout.simple_list_item_1, dataList);
34 listview.setAdapter(adapter);
35 listview.setOnItemLongClickListener(new OnItemLongClickListener() {
36 @SuppressLint("NewApi")
37 @Override
38 public boolean onItemLongClick(AdapterView<?> parent, View view,
39 int position, long id) {
40 if (mActionMode != null) {
41 return false;
42 }
43 //显示ActionMode
44 mActionMode = startActionMode(mActionModeCallback);
45 //标记选中项的下表
46 mActionMode.setTag(position);
47 //标记ListView为可选状态
48 view.setSelected(true);
49 return true;
50 }
51 });
52 }
53 public List<String> getData()
54 {
55 List<String> data=new ArrayList<String>();
56 for(int i=0;i<8;i++)
57 {
58 data.add("item"+i);
59 }
60 return data;
61 }
62
63 private ActionMode.Callback mActionModeCallback=new Callback() {
64
65 @Override
66 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
67 //刷新菜单列表的时候被调用,但是一般无需刷新
68 return false;
69 }
70
71 @Override
72 public void onDestroyActionMode(ActionMode mode) {
73 //销毁ActionMode
74 mActionMode = null;
75 }
76
77 @Override
78 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
79 //创建ActionMode
80 //使用资源文件填充
81 MenuInflater inflater = mode.getMenuInflater();
82 inflater.inflate(R.menu.contextmenu, menu);
83 return true;
84 }
85
86 @Override
87 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
88 //获取选项中下表
89 int position=(Integer)mode.getTag();
90 switch (item.getItemId()) {
91 case R.id.context_copy:
92 Toast.makeText(ActionModeMenu1.this, "copy "+dataList.get(position), Toast.LENGTH_SHORT).show();
93 //finish退出ActionMode模式
94 mode.finish();
95 return true;
96 case R.id.context_delete:
97 Toast.makeText(ActionModeMenu1.this, "delete "+dataList.get(position), Toast.LENGTH_SHORT).show();
98 mode.finish();
99 return true;
100 case R.id.context_edit:
101 Toast.makeText(ActionModeMenu1.this, "edit " +dataList.get(position), Toast.LENGTH_SHORT).show();
102 mode.finish();
103 return true;
104 default:
105 return false;
106 }
107 }
108 };
109
110
111 }
 
 

results show, Android4.0:

 

 

 

PopupMenu

 

PopupMenu , the pop-up menu, a pop-up modal form showing style menu, tie In a certain View, and generally appear in the lower bound of View (if there is space below).

 

Use PopupMenu steps:

 
      
  1. constructor through PopupMenu PopupMenu instantiate an object, you need to pass an object as well as the current context bound View.
  2.   
  3. call PopupMenu.setOnMenuItemClickListener () to set a PopupMenu option selected events.
  4.   
  5. Use MenuInflater.inflate () method to load an XML file to PopupMenu.getMenu () in.
  6.   
  7. when needed to call PopupMenu.show () method to display.
  8.  
 

Example:

 
  
 1     public void showPopup(View v){ 
2 PopupMenu popup=new PopupMenu(MainActivity.this, v);
3 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
4
5 @Override
6 public boolean onMenuItemClick(MenuItem item) {
7 switch (item.getItemId()) {
8 case R.id.context_copy:
9 Toast.makeText(MainActivity.this, "select copy ", Toast.LENGTH_SHORT).show();
10 return true;
11 case R.id.context_delete:
12 Toast.makeText(MainActivity.this, " select delete ", Toast.LENGTH_SHORT).show();
13 return true;
14 case R.id.context_edit:
15 Toast.makeText(MainActivity.this, " select edit ", Toast.LENGTH_SHORT).show();
16 return true;
17 default :
18 return false;
19 }
20 }
21 });
22 popup.getMenuInflater().inflate(R.menu.contextmenu,popup.getMenu());
23 popup.show();
24 }
 
 

results show, Android4.0:

 

 

source download

 

Summary

 

above under Menu to explain the use of the Android. Because all of the above examples were done in a project, so some low version, you need to change the minimum support AndroidManifest.xml file versions can be changed to 8. In the present actual development, it is best to use only high version of ActionBar, ActionMode, PopupMenu better.

 

 

Please support the original, respect for the original, reproduced please indicate the source. Thank you.

没有评论:

发表评论