"Turn" Android instance profile note (ii)--explain the development process of andriod with an example and introduce the Android menu mechanism with Noteslist as an example

Source: Internet
Author: User

Original URL: http://kb.cnblogs.com/page/78304/

Brief introduction

Android offers three menu types, the options menu,context menu,sub menu, respectively.

The Options menu is displayed by pressing the home button, and the context menu needs to be displayed on the view after pressing 2s. Both menus have a submenu that can be added to, and submenus cannot be nested. Options menu can display up to 6 menu options at the bottom of the screen, called Iconmenu,icon menu, which cannot have the checkable option. More than 6 of the menu items are paged out with more icon menu, called expanded menu. The Options menu is generated by the activity's Oncreateoptionsmenu, which is only called when the menu is first generated. Any idea to change the options menu can only be implemented in Onprepareoptionsmenu, which is called before the menu is displayed. The onoptionsitemselected is used to process the selected menu item.

The context menu is bound to a specific view and is used to register the context menu for a view in the activity type Registerforcontextmenu. The context menu will call Oncreatecontextmenu to generate the menu before it is displayed. The oncontextitemselected is used to process the selected menu item.

Android also provides the ability to group menu items into a single group, so that you can set menu properties by calling Setgroupcheckable,setgroupenabled,setgroupvisible. Without having to set it separately.

Options Menu

The Notepad menu is available in the Options menu and the two menus of the context menu. Let's start with the Oncreateoptionsmenu function that generates the Options menu.

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 way to insert a menu item with the ID of the menu item Menu_item_insert. The interesting thing is the following code:

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

What is the use of this? In fact, this is a dynamic menu technology (also a bit like plug-in mechanism), if an activity, its type is "Android.intent.category.ALTERNATIVE", the data is "vnd.android.cursor.dir/ Vnd.google.note, the system will add a menu item to the activity. When viewed in Androidmanfest.xml, no activity is eligible, so this code does not dynamically add any menu items.

To verify the above analysis, we can do an experiment to make changes in the Androidmanfest.xml to see if the menu item will be generated dynamically.

Experiment One

First, let's create a new activity as the target activity, called helloandroid, with no function, which is to display an interface.

public class Helloandroid extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
This.setcontentview (R.layout.main);
}
}

It corresponds to the layout interface XML file 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 the Androidmanfest.xml, add the following configuration, let Helloandroid meet the above two conditions:

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

OK, run the next try, ah, still no dynamic menu items to join Ah! What's going on here? Look at the code and found that the original is the ghost of Onprepareoptionsmenu! This function runs after oncreateoptionsmenu, the following code, because Menu.category_alternative is pointing to the same group, so the menu items set in the Oncreateoptionsmenu are overwritten, And because Onprepareoptionsmenu did not give menu.category_alternative attached new value, so menu.category_alternative or empty.

Intent Intent = new Intent (null, URI);
Intent.addcategory (intent.category_alternative);
Menu.addintentoptions (menu.category_alternative, 0, 0, NULL, specifics, intent, 0,items);

OK, so we temporarily put these words to comment off, of course, can not comment on these words, in the oncreateoptionsmenu to change GroupID, will menu.category_alternative to Menu.first, the other line, But be careful not to change to Menu.none, this will be covered out.

Menu.add (0, Menu_item_insert, 0, R.string.menu_insert)
. Setshortcut (' 3 ', ' a ')
. SetIcon (Android. R.drawable.ic_menu_add);

The added menu. Because the Menu.none is also 0. After running, you can see the dynamic menu coming out!

The above Options menu is generated when there is no log list selected on the Noteslist interface, if you select a log and then click "Menu", the resulting options menu is as follows:

Alas, the dynamic addition of two menu item "Edit Note" and "edit title", this is how to join the dynamic? This is the credit of Onprepareoptionsmenu.

Uri uri = Contenturis.withappendedid (Getintent (). GetData (), Getselecteditemid ());

First gets the selected log (if no selection, the URI is empty)

intent[] specifics = new INTENT[1];
Specifics[0] = new Intent (Intent.action_edit, URI);
menuitem[] items = new Menuitem[1];

A intent is then created for the selected log, the action type is Intent.action_edit, and the data is the URI of the selected log. A "EDIT note" menu item is created for the selected log.

Intent Intent = new Intent (null, URI);
Intent.addcategory (intent.category_alternative);
Menu.addintentoptions (menu.category_alternative, 0, 0, NULL, specifics, intent, 0,
Items);

This is similar to the above Oncreateoptionsmenu function, used to dynamically add menu items, if an activity, whose type is "Android.intent.category.ALTERNATIVE", the data is " Vnd.android.cursor.item/vnd.google.note, the system will add a menu item to the activity. After viewing it in Androidmanfest.xml, titleeditor the activity is eligible, and the system adds a menu item "Edit title" for the Titleeditor activity dynamically.

else {
Menu.removegroup (menu.category_alternative);
}

If the log list is empty, the menu item with the group number menu.category_alternative is removed from the menu and only the ADD note menu item is left.

Handle the "Selected menu item" event

The handling of the menu item selection event is very simple, done by onoptionsitemselected, which simply calls StartActivity (new Intent (Intent.action_insert, Getintent (). GetData ())); The intent operation type is Intent.action_insert, and the data is the URI of the log list, which is "content://com.google.provider.notepad/notes"

@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

Another menu---Context menu is described below, which is implemented by overloading the Oncreatecontextmenu function. First verify that a log is selected in the Log list, and then return directly if not selected. The cursor points to the selected log entry.

cursor cursor = (cursor) getlistadapter (). GetItem (info.position);
if (cursor = = NULL) {
For some reason the requested item isn ' t available, does nothing
Return
}

Then, set the title of the context menu to the journal title

Setup the menu header
Menu.setheadertitle (cursor.getstring (column_index_title));

Finally, add a menu item to the context menus

ADD a menu item to delete the note
Menu.add (0, Menu_item_delete, 0, R.string.menu_delete);

The event handling that is selected for the context menu item is implemented by overloading oncontextitemselected.

Switch (Item.getitemid ()) {
Case Menu_item_delete: {
Delete the note, the context menu is for
Uri Noteuri = Contenturis.withappendedid (Getintent (). GetData (), info.id);
Getcontentresolver (). Delete (Noteuri, NULL, NULL);
return true;
}
}
return false;
}

For the deletion of the log, first call Contenturis.withappendedid (Getintent (). GetData (), info.id), to splice the URI of the log to be deleted. Then Getcontentresolver (). Delete (Noteuri, NULL, NULL); Call the underlying content provider to delete the log.

Experiment Two

To do a simple experiment, add a context menu item based on the above code. First, add a context menu item to the Oncreatecontextmenu function:

Menu.add (0,menu_item_insert,0,r.string.menu_insert);

Then add a process to it in the oncontextitemselected function:

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 experimental results are as follows:


"Turn" Android instance profile note (ii)--explain the development process of andriod with an example and introduce the Android menu mechanism with Noteslist as an example

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.