Menu customization list encapsulated by Android

Source: Internet
Author: User

For a self-encapsulated menu custom menu list, you must first create a class to inherit the activity, so that the class that calls the custom menu can be directly inherited.

Package com. CNG;

Import java. util. arraylist;
Import java. util. hashmap;

Import Android. App. activity;
Import Android. App. alertdialog;
Import Android. App. tabactivity;
Import Android. content. dialoginterface;
Import Android. content. sharedpreferences;
Import Android. content. dialoginterface. onkeylistener;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. Preference. preferencemanager;
Import Android. util. log;
Import Android. View. keyevent;
Import Android. View. Menu;
Import Android. View. view;
Import Android. widget. adapterview;
Import Android. widget. gridview;
Import Android. widget. simpleadapter;
Import Android. widget. adapterview. onitemclicklistener;

Public class menuactivityadapter extends Activity
{
Alertdialog menudialog; // menu Dialog
Gridview menugrid;
View menuview;
Final private int menusettings = menu. first;
Private Static final int req_system_settings = 0;


/** Menu image **/
Int [] menu_image_array = {R. drawable. xuetang_light, R. drawable. tizhong_light,
R. drawable. maibo_light,
R. drawable. tiwen_light, R. drawable. xueyang_light,
R. drawable. naodian_light, R. drawable. xindian_light,
R. drawable. xueya_light,
R. drawable. denglu_light,
R. drawable. shezhi_light };
/** Menu text **/
String [] menu_name_array = {"","","","","","",
"","","",""};

@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate); Women's brand ranking


Menuview = view. Inflate (this, R. layout. gridview_menu, null );
// Create alertdialog
Menudialog = new alertdialog. Builder (this). Create ();
Menudialog. setview (menuview );
Menudialog. setonkeylistener (New onkeylistener (){
Public Boolean onkey (dialoginterface dialog, int keycode,
Keyevent event ){
If (keycode = keyevent. keycode_menu) // listen to the button
Dialog. Dismiss ();
Return false;
}
});

Menugrid = (gridview) menuview. findviewbyid (R. Id. gridview );
Menugrid. setadapter (getmenuadapter (menu_name_array, menu_image_array ));
/** Monitor menu options **/
Menugrid. setonitemclicklistener (New onitemclicklistener (){
Public void onitemclick (adapterview <?> Arg0, view arg1, int arg2,
Long arg3 ){
Intent intent = new intent ();
Switch (arg2 ){

Case 0: // search
Intent. setclass (menuactivityadapter. This, glucoselist. Class );
Startactivity (intent );
Menudialog. Dismiss ();
Break;
Case 1: // search
Intent. setclass (menuactivityadapter. This, weightlist. Class );
Startactivity (intent );
Menudialog. Dismiss ();
Break;
Case 2: // search
Intent. setclass (menuactivityadapter. This, maibolist. Class );
Startactivity (intent );
Menudialog. Dismiss ();
Break;
Case 3: // search
Intent. setclass (menuactivityadapter. This, tiwenlist. Class );
Startactivity (intent );
Menudialog. Dismiss ();
Break;
Case 4: // search
Intent. setclass (menuactivityadapter. This, xueyanglist. Class );
Startactivity (intent );
Menudialog. Dismiss ();
Break;
Case 5: // search
Intent. setclass (menuactivityadapter. This, naodianlist. Class );
Startactivity (intent );
Menudialog. Dismiss ();
Break;
Case 6: // search
Intent. setclass (menuactivityadapter. This, xinlv. Class );
Startactivity (intent );
Menudialog. Dismiss ();
Break;
Case 7: // search
Intent. setclass (menuactivityadapter. This, xueyalist. Class );
Startactivity (intent );
Menudialog. Dismiss ();
Break;
Case 8: // search
Intent. setclass (menuactivityadapter. This, denglulist. Class );
Startactivity (intent );
Menudialog. Dismiss ();
Break;
Case 9: // search
Startactivityforresult (new intent (menuactivityadapter. This, settings. Class), req_system_settings );
Menudialog. Dismiss ();
Break;
}


}
});
}
@ Override
Public Boolean oncreateoptionsmenu (menu ){
Menu. Add ("menu"); // You must create an item.
Return super. oncreateoptionsmenu (menu );
}

Private simpleadapter getmenuadapter (string [] menunamearray,
Int [] imageresourcearray ){
Arraylist For (INT I = 0; I <menunamearray. length; I ++ ){
Hashmap <string, Object> map = new hashmap <string, Object> ();
Map. Put ("itemimage", imageresourcearray [I]);
Map. Put ("itemtext", menunamearray [I]);
Data. Add (MAP );
}
Simpleadapter simperadapter = new simpleadapter (this, data,
R. layout. item_menu, new string [] {"itemimage", "itemtext "},
New int [] {R. Id. item_image, R. Id. item_text });
Return simperadapter;
}
@ Override
Public Boolean onmenuopened (INT featureid, menu ){
If (menudialog = NULL ){
Menudialog = new alertdialog. Builder (this). setview (menuview). Show ();
} Else {
Menudialog. Show ();
}
Return false; // if the return value is true, the system menu is displayed.
}
// Result returned on the settings page
Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
If (requestcode = req_system_settings)
{
// Obtain the preference values in the preferenceactivity interface.
String loginswitchkey = getresources (). getstring (R. String. auto_login_switch_key );
String customizelist1key = getresources (). getstring (R. String. customize_main_list1_key );
String serveraddresskey = getresources (). getstring (R. String. server_address_key );
// Obtain the entire application Program Sharedpreferences
Sharedpreferences settings = preferencemanager. getdefasharsharedpreferences (this );
Boolean updateswitch = settings. getboolean (loginswitchkey, true );
String updatefrequency = settings. getstring (customizelist1key, "weight ");
String serveraddress = settings. getstring (serveraddresskey, "172.16.10.254 ");
// Print the result
Log. V ("checkboxpreference_main", updateswitch. tostring ());
Log. V ("listpreference_main", updatefrequency );
Log. V ("edittextpreference_main", serveraddress );

}
Else
{
// Other intent returned results
}
}
}
Gridview_menu.xmlCode

<? 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"
>
<Gridview
Android: Id = "@ + ID/gridview"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: numcolumns = "5"
Android: verticalspacing = "1dip"
Android: horizontalspacing = "1dip"
Android: stretchmode = "columnwidth"
Android: gravity = "center"
/>

</Linearlayout>

Item_menu.xml code


Android: Id = "@ + ID/relativelayout_item" Freight expert
Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"
Android: paddingbottom = "1dip">
Android: layout_centerhorizontal = "true" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
Android: layout_centerhorizontal = "true" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: text = "option">

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.