Display jump and implicit jump instances of the selected menu in android

Source: Internet
Author: User

I checked a lot of information and found that it is still incomplete. Simply sort it out, at least make sure that my practice is correct, so as not to mislead readers, but also to record myself!

Introduction

Android provides three menu types: options menu, context menu, and sub menu.

The options menu is displayed by pressing the home key. The context menu needs to be displayed after pressing the top 2 S on The view. Both menus can be used as submenus. submenus cannot be nested. The options menu can only display six menu items at the bottom of the screen, which is called iconmenu. The icon menu cannot have checkable items. More than 6 menu items are called expanded menu. Options menu is generated by onCreateOptionsMenu of the activity. This function is only called when the menu is generated for the first time. Any attempt to change the options menu can only be made in onPrepareOptionsMenu. This function will be called before the menu is displayed. OnOptionsItemSelected is used to manage the selected menu items.

Context menu is bound with the view of an individual. In the activity, registerForContextMenu is used to register the context menu for a view. Before the context menu is displayed, the city calls onCreateContextMenu to generate the menu. OnContextItemSelected is used to manage the selected menu items.

Android also provides the menu item traveling grouping function, which can share the similar menu items in the same group, so that you can call setGroupCheckable, setGroupEnabled, setGroupVisible is used to set menu properties without the need to set menu properties.

Options Menu

The options menu and context menu are used in Notepad. First, let's look at the onCreateOptionsMenu function that generates the options menu.

Copy codeThe Code is as follows: menu. add (0, MENU_ITEM_INSERT, 0, R. string. menu_insert)
. SetShortcut ('3', 'A ')
. SetIcon (android. R. drawable. ic_menu_add );

This is a standard method for inserting a menu item. The menu item id is MENU_ITEM_INSERT. The following code is interesting:

Copy codeThe Code is as follows: Intent intent = new Intent (null, getIntent (). getData ());
Intent. addCategory (Intent. CATEGORY_ALTERNATIVE );
Menu. addIntentOptions (Menu. CATEGORY_ALTERNATIVE, 0, 0,
New ComponentName (this, NotesList. class), null, intent, 0, null );

Where can this be used? In fact, this is a kind of dynamic menu technique (also a bit like a plug-in mechanism). If an activity, its type is "android. intent. category. ALTERNATIVE ", data is" vnd. android. cursor. dir/vnd. google. note, the system will add a menu item for this activity. After viewing in androidmanfest. xml, no activity meets the conditions. Therefore, this code does not dynamically add any menu items.

In order to verify the above analysis, we can perform a verification and modify it in androidmanfest. xml to see if it will generate a menu item.

Lab 1

First, create a new activity as the target activity named HelloAndroid. There is no function, that is, an interface.

Copy codeThe Code is as follows: public class HelloAndroid extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
This. setContentView (R. layout. main );
}
}

The XML file corresponding to the layout interface is as follows:

Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: id = "@ + id/TextView01"/>

<Button android: id = "@ + id/Button01" android: layout_height = "wrap_content" android: layout_width = "fill_parent" android: text = "@ string/txtInfo"> </Button>
</LinearLayout>

Then modify androidmanfest. xml to participate in the following configuration, so that HelloAndroid is satisfied with the preceding two conditions:

Copy codeThe Code is as follows: <activity android: name = "HelloAndroid" android: label = "@ string/txtInfo">
<Intent-filter>
<Action android: name = "com. android. notepad. action. HELLO_TEST"/>
<Category android: name = "android. intent. category. ALTERNATIVE"/>
<Data android: mimeType = "vnd. android. cursor. dir/vnd. google. note"/>
</Intent-filter>
</Activity>

Okay, run it and try it. Well, there are still no dynamic menu items! What's going on? Check the code and send it now. It turns out to be the ghost of onPrepareOptionsMenu! This function is run after onCreateOptionsMenu. In the following code. CATEGORY_ALTERNATIVE points to the same group, so the Menu items set in onCreateOptionsMenu are overwritten, because onPrepareOptionsMenu does not give the Menu. CATEGORY_ALTERNATIVE has a new value, so Menu. CATEGORY_ALTERNATIVE is null.

Copy codeThe Code is as follows: Intent intent = new Intent (null, uri );
Intent. addCategory (Intent. CATEGORY_ALTERNATIVE );
Menu. addIntentOptions (Menu. CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0, items );

Okay, let's temporarily release the following statements. Of course, you can also ignore them and change the groupid number in onCreateOptionsMenu, that is, the Menu. CATEGORY_ALTERNATIVE to change Menu. first, you can do the rest, but do not change the menu. none.

Copy codeThe Code is as follows: menu. add (0, MENU_ITEM_INSERT, 0, R. string. menu_insert)
. SetShortcut ('3', 'A ')
. SetIcon (android. R. drawable. ic_menu_add );

The menu to add. Because menu. none is also 0. After the operation, you can see the dynamic menu!

The options menu below is generated when no diary list is selected on the NotesList interface. If a diary is selected first, click "menu ", the generated options menu is as follows:

Daily routine
On a quiet night, I am alone, a little empty, a little miserable. Sitting under the stars, looking up and looking up at the beautiful sky, the feeling is real but illusory, shining, it seems that there are still some beats. Everything in the United States is always in an instant, just like a mirage. It's just a flash in the twinkling of an eye. When the sky becomes bright, and the stars have already retreated together ......

Alas, two menu items "Edit note" and "Edit title" are added to the dynamic mode. How is the dynamic mode involved? This is what onPrepareOptionsMenu does.

Copy codeThe Code is as follows: Uri uri = ContentUris. withAppendedId (getIntent (). getData (), getSelectedItemId ());

First, obtain the selected diary (if no selected, the uri is blank)

Copy codeThe Code is as follows: Intent [] specifics = new Intent [1];
Specifics [0] = new Intent (Intent. ACTION_EDIT, uri );
MenuItem [] items = new MenuItem [1];

Create an intent for the selected log, and set the operation type to Intent. ACTION_EDIT. The number of records is the URI of the selected log. Therefore, an "Edit note" menu item is created for the selected log.

Copy codeThe Code is as follows: Intent intent = new Intent (null, uri );
Intent. addCategory (Intent. CATEGORY_ALTERNATIVE );
Menu. addIntentOptions (Menu. CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0,
Items );

These statements are similar to the following onCreateOptionsMenu functions. You can add a menu item by using a State animation. If an activity is of the "android. intent. category. ALTERNATIVE ", data is" vnd. android. cursor. item/vnd. google. note, the system will add a menu item for this activity. After viewing in androidmanfest. xml, the activity TitleEditor meets the conditions. Therefore, the system adds a menu item "Edit title" to the activity state of TitleEditor ".

Copy codeThe Code is as follows: else {
Menu. removeGroup (Menu. CATEGORY_ALTERNATIVE );
}

If the diary list is empty, delete the Menu items with the group number Menu. CATEGORY_ALTERNATIVE from the Menu, and only the "Add note" Menu items are left.

Manage the "selected menu items" Event

The reason for the selected event in the menu item is very simple. It is completed through onOptionsItemSelected. Here we just use startActivity (new Intent (Intent. ACTION_INSERT, getIntent (). getData (); the intent type is Intent. ACTION_INSERT: The URI of the log list, that is, "content: // com. google. provider. notePad/notes"

Copy codeThe Code is as follows: @ Override
Public boolean onOptionsItemSelected (MenuItem item ){
Switch (item. getItemId ()){
Case MENU_ITEM_INSERT:
// Launch activity to insert a new item
StartActivity (new Intent (Intent. ACTION_INSERT, getIntent (). getData ()));
Return true;
}
Return super. onOptionsItemSelected (item );
}

Context Menu

Next we will analyze another menu-context menu, which is implemented by reloading the onCreateContextMenu function. First, confirm that one of the diaries is selected. If no, the system returns the result directly. Cursor points to the selected log entry.

Copy codeThe Code is as follows: Cursor cursor = (Cursor) getListAdapter (). getItem (info. position );
If (cursor = null ){
// For some reason the requested item isn't available, do nothing
Return;
}

Then, set the title of the context menu as the diary title.

Copy codeThe Code is as follows: // Setup the menu header
Menu. setHeaderTitle (cursor. getString (COLUMN_INDEX_TITLE ));

Add a menu item to the context menu.

Copy codeThe Code is as follows: // Add a menu item to delete the note
Menu. add (0, MENU_ITEM_DELETE, 0, R. string. menu_delete );

For the event processing area selected in the context menu item, the onContextItemSelected reality is reloaded.

Copy codeThe Code is as follows: switch (item. getItemId ()){
Case MENU_ITEM_DELETE :{
// Delete the note that the context menu is
Uri noteUri = ContentUris. withAppendedId (getIntent (). getData (), info. id );
GetContentResolver (). delete (noteUri, null, null );
Return true;
}
}
Return false;
}

For the addition and deletion of the diary, call ContentUris first. withAppendedId (getIntent (). getData (), info. id. then getContentResolver (). delete (noteUri, null, null); Use the Content Provider at the calling layer to delete this log.

Lab 2

Add a context menu item based on the above Code. First, add a context menu item to the onCreateContextMenu function:

Copy codeThe Code is as follows: menu. add (0, MENU_ITEM_INSERT, 0, R. string. menu_insert );

Add a processing process to the onContextItemSelected function:

Copy codeThe Code is as follows: case MENU_ITEM_INSERT:
{
New AlertDialog. Builder (this). setIcon (R. drawable. app_notes)
. SetTitle (R. string. app_name). setMessage (R. string. error_message). setPositiveButton (R. string. button_ OK, new OnClickListener (){

Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub

}

}). Show ();
Return true;
}

The verification result is as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.