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;
            }
        });
    }
}

posted @ 2013-10-23 15:29  skyko  阅读(2464)  评论(0编辑  收藏  举报