Chapter 7 pop-up menu (PopUp Menus), popupmenus

Source: Internet
Author: User

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.

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.