ANDROID笔记:PopupMenu的用法

package com.example.android_menu;



import android.annotation.SuppressLint;

import android.app.Activity;

import android.os.Bundle;

import android.view.MenuInflater;

import android.view.MenuItem;

import android.view.View;

import android.widget.PopupMenu;

import android.widget.PopupMenu.OnMenuItemClickListener;

import android.widget.Toast;



public class PopupMenuActivity extends Activity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.othermeun);

    }



    @SuppressLint("NewApi")

    public void btnClick(View button) {

        final PopupMenu popupMenu = new PopupMenu(getApplicationContext(),

                button);

        MenuInflater menuInflater = new MenuInflater(getApplicationContext());

        menuInflater.inflate(R.menu.mymenu, popupMenu.getMenu());

        popupMenu.show();

        popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override

            public boolean onMenuItemClick(MenuItem item) {

                Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT)

                        .show();

                popupMenu.dismiss();

                return false;

            }

        });

    }

}

ANDROID笔记:PopupMenu的用法

你可能感兴趣的:(android)