Chapter 7 pop-up menu (PopUp Menus), popupmenus
Category: C #, Android, VS2015;
Created on: 1. Introduction
Function Description: click the button to bring up the menu. When you select a menu item, the MenuItemClick event is triggered and the pop-up menu disappears. If you click it outside the menu, the pop-up menu disappears directly. When a menu disappears, A DismissEvent event is triggered (this event can be used for subsequent processing when the menu disappears ). Ii. Example 7 -- Demo07PopupMenu
1. Running Effect
2. Add menu items
Add a menu Sub-folder under the Resources folder and add a file named demo07_popup_menu.xml under the sub-Folder:
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item1" android:title="item 1" /> <item android:id="@+id/item1" android:title="item 2" /> <item android:id="@+id/item1" android:title="item 3" /></menu>
3. Add demo07_PopupMenu.axml
<?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"> <Button android:id="@+id/popupButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/showPopup" /></LinearLayout>
Save all files.
4. Add Demo07PopupMenu. cs
Using Android. app; using Android. OS; using Android. widget; namespace ch05demos. srcActivity {[Activity (Label = "Demo07PopupMenu")] public class Demo07PopupMenu: Activity {protected override void OnCreate (Bundle savedInstanceState) {base. onCreate (savedInstanceState); SetContentView (Resource. layout. demo07_Popup); Button btn = FindViewById <Button> (Resource. id. popupButton); btn. click + = (s, arg) =>{ PopupMenu menu = new PopupMenu (this, btn); menu. inflate (Resource. menu. demo07_popup_menu); menu. menuItemClick + = (sender, args) => {string str = string. format ("You selected: {0}", args. item); Toast. makeText (this, str, ToastLength. short ). show () ;}; menu. dismissEvent + = (sender, args) =>{// after the menu disappears, you can perform some subsequent processing in this event}; menu. show ();};}}}
Observe the running effect.