Hướng dẫn tạo menu trong Android
@2011 Mihail L. Sichitiu 1 Android Introduction GUI Menu Many thanks to Jun Bum Lim for his help with this tutorial. @2011 Mihail L. Sichitiu 2 Goal Create an application that supports options/sub/context menus <option menu> <sub-menu> <context menu> Automatically fill “Hi!” in the EditText Plus menu will also open a sub-menu Display messages when a menu clicked @2011 Mihail L. Sichitiu 3 Menu Composition Plus Home Pre Next <option menu> Sub1 Sub2 Hi Hola Hello <sub-menu> <context menu from EditText> Long press in EditText @2011 Mihail L. Sichitiu 4 Create HelloMenu Project Create the two TextViews and an EditText Create “menu” folder in res/ Create menu.xml in res/menu/ (New > Other > Android XML File) Create context_menu.xml in res/menu/ @2011 Mihail L. Sichitiu 5 res/menu/menu.xml <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menuItemPlus" android:title="@string/plus" android:icon="@drawable/plus" > <menu> <item android:id="@+id/menuItemSub1" android:title="@string/sub1"></item> <item android:id="@+id/menuItemSub2" android:title="@string/sub2"></item> </menu> </item> <item android:icon="@drawable/home" android:id="@+id/menuItemHome" android:title="@string/home"></item> <item android:icon="@drawable/pre" android:id="@+id/menuItemPre" android:title="@string/pre"></item> <item android:icon="@drawable/next" android:id="@+id/menuItemNext" android:title="@string/next"></item> </menu> Sub-menu items Option menu items Define Option menu and sub-menu @2011 Mihail L. Sichitiu 6 res/menu/context.xml <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menuItemHi" android:title="@string/hi_msg"></item> <item android:id="@+id/menuItemHola" android:title="@string/hola_msg"></item> <item android:id="@+id/menuItemHello" android:title="@string/hello_msg"></item> </menu> Define context menu for EditText @2011 Mihail L. Sichitiu 7 res/values/strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Click Menu Button ! </string> <string name="app_name">Android Menu Example</string> <string name="plus">Plus</string> <string name="pre">Pre</string> <string name="next">Next</string> <string name="home">Home</string> <string name="sub1">Sub1</string> <string name="sub2">Sub2</string> <string name="hi_msg">Hi !</string> <string name="hola_msg">Hola !</string> <string name="hello_msg">Hello !</string> </resources> Define constant strings used in the application @2011 Mihail L. Sichitiu 8 icons Place icons used in menu.xml in res/drawable/ icons Download icons at: http://www4.ncsu.edu/~mlsichit/UCAB/resources.html @2011 Mihail L. Sichitiu 9 Inflating a option menu resource Inflating a menu resource (menu.xml) by adding onCreateOptionsMenu(Menu menu) in the main Activity. Menu items in menu.xml will appear when the user touches the MENU button public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } @2011 Mihail L. Sichitiu 10 Response to user action Response to menu click events by overriding onOptionsItemSelected(Menu menu) in the main Activity. @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menuItemPlus: Toast.makeText(this, "Plus Button Clicked !", Toast.LENGTH_SHORT).show(); Log.i(TAG,"menuItemPlus"); return true; : : case R.id.menuItemNext: Toast.makeText(this, "Next Button Clicked !", Toast.LENGTH_SHORT).show(); Log.i(TAG,"menuItemNext"); return true; } return false; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menuItemPlus: Toast.makeText(this, "Plus Button Clicked !", Toast.LENGTH_SHORT).show(); Log.i(TAG,"menuItemPlus"); return true; : : case R.id.menuItemNext: Toast.makeText(this, "Next Button Clicked !", Toast.LENGTH_SHORT).show(); Log.i(TAG,"menuItemNext"); return true; } return false; } . onCreateContextMenu(). @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu (menu, v, menuInfo); MenuInflater. touches the MENU button public boolean onCreateOptionsMenu (Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R .menu. menu, menu) ; return