Chapter 2 attracting your attention-UI programming (8), chapter 2 ui

Source: Internet
Author: User

Chapter 2 attracting your attention-UI programming (8), chapter 2 ui
2.4 call for the Menu key-Menu

Friends who use Android phones should be familiar with the "Menu" Key of their phones. It is convenient and convenient to use, and does not need to occupy the application interface. Thanks to these features, many applications now use the Menu-Menu key.

There are several Menu types, each of which has its own presentation form. They are used in different occasions. Here we will introduce them one by one.

 

1) normal Menu (option Menu)

This is the most commonly used and most common menu. It is the menu that pops up when you click the menu button on the device. It can display up to 6 items. If there are more than 6 items, the "more" option is automatically displayed. It is also easy to create. it overwrites the onCreateOptionsMenu (Menu menu) method in the Activity.

@ Override

Public boolean onCreateOptionsMenu (Menu menu ){

Menu. add (0, 1, 1, "add ");

Menu. add (0, 2, 2, "delete ");

Return super. onCreateOptionsMenu (menu );

}

 

There are four parameters in the add method of Menu:

The first int type group ID parameter represents the group concept. You can group several menu items to better manage your menu buttons in a group.

The item ID parameter of the second int type represents the project number. This parameter is very important. An item ID corresponds to an option in a menu. This item ID is used to determine which option you are using when using the menu later.

 The third 'order id' parameter of 'int' type represents the order in which menu items are displayed. The default value is 0, indicating that the display order of the menu is displayed according to the display order of add.

 The fourth String type title parameter, indicating the text displayed in the option.

After pressing the Menu key, the effect is 2-24:

Figure 2-24 use of common menus

 

Of course, we can also set a separate icon for each menu item:

Menu. add (0, 1, 1, "add"). setIcon (R. drawable. add_no );

Menu. add (0, 2, 2, "delete"). setIcon (R. drawable. performance_no );

 

The results are as follows:

Figure 2-25 add an icon to Menu

 

In this way, there are two menu options: "add" and "delete. To add a click event, override the onOptionsItemSelected (MenuItem item) method.

@ Override

Public boolean onOptionsItemSelected (MenuItem item ){

If (item. getItemId () = 1 ){

Toast t = Toast. makeText (this, "you selected add menu", Toast. LENGTH_SHORT );

T. show ();

} Else if (item. getItemId () = 2 ){

Toast t = Toast. makeText (this, "you selected the delete menu", Toast. LENGTH_SHORT );

T. show ();

}

Return true;

}

 

2) SubMenu)

If Menu is the level 1 button, SubMenu is the level 2 button, which displays the menus of multiple levels by grouping the same functions.

SubMenu is also easy to use. Add a few sentences to the onCreateOptionsMenu (Menu menu) method of the first code, as follows:

@ Override

Public boolean onCreateOptionsMenu (Menu menu ){

Menu. add (0, 1, 1, "add ");

Menu. add (0, 2, 2, "delete ");

SubMenu subMenu = menu. addSubMenu (0, 3, 3, "modify ");

SubMenu. add (1, 4, 1, "Modify user name ");

SubMenu. add (1, 5, 2, "Password Change ");

Return true;

}

 

After you click "modify", a sub-menu is displayed, which has two sub-options: "Change User Name" and "Change Password". The effect is 2-26:

Figure 2-26 Use of sub-menus

 

3) Context Menu)

Context Menu is similar to a shortcut Menu on a computer that is popped up after a View is long pressed. Here is a simple example.

First, define a button in the layout file:

<? Xml version = "1.0" encoding = "UTF-8"?>

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent">

<Button

Android: id = "@ + id/test"

Android: layout_centerInParent = "true"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: text = "my buttons"/>

</RelativeLayout>

 

Then we register the two buttons in the onCreate method of the Activity. The Code is as follows:

Private Button myButton;

 

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

MyButton = (Button) findViewById (R. id. my_button1 );

RegisterForContextMenu (myButton );

}

 

After registration, we can overwrite the onCreateContextMenu method. In this method, we can implement the long-press event of the b1 and b2 buttons. The Code is as follows:

@ Override

Public void onCreateContextMenu (ContextMenu menu, View v,

ContextMenuInfo menuInfo ){

If (v = myButton ){

Toast. makeText (DialogActivity. this, "Long press event", Toast. LENGTH_SHORT). show ();

}

Super. onCreateContextMenu (menu, v, menuInfo );}

 

We long press the button to see the effect, as shown in Figure 2-27:

Figure 2-27 use of context menu

 

We use the above three menus, which have their own characteristics and are applied to different occasions.

In fact, we often use xml files instead of creating menus through hard encoding. This method can separate the code from the file to make the entire Code look clearer. It is convenient and quick to create. The following is a simple example.

First, create a folder named menu under the res/directory, and then create an xml file named menu_xml_file.xml under the folder. The Code is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>

<Menu xmlns: android = "http://schemas.android.com/apk/res/android">

<Group android: id = "@ + id/grout_main">

<Item android: id = "@ + id/menu_1"

Android: title = "Menu1"/>

<Item android: id = "@ + id/menu_2"

Android: title = "Menu2"/>

</Group>

</Menu>

 

Override the onCreateOptionsMenu (Menu menu) method in the Activity. The Code is as follows:

@ Override

Public boolean onCreateOptionsMenu (Menu menu ){

MenuInflater inflater = getMenuInflater ();

Inflater. inflate (R. menu. menu_xml_file, menu );

Return true;

}

 

In this way, the Menu is created, as shown in Figure 2-28:

Figure 2-28 menu for creating an xml file

 

Like before, we can also listen to their click events by ID.

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.