Android 上下文菜单 PopupMenu

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) this.findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                PopupMenu poMenu = new PopupMenu(MainActivity.this, v);
                poMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        // TODO Auto-generated method stub

                        switch (item.getItemId()) {
                        case R.id.action_add:
                            Toast.makeText(MainActivity.this, "添加被点击", 1).show();

                            break;
                        case R.id.action_delete:
                            Toast.makeText(MainActivity.this, "添加被点击", 1).show();
                            break;

                        default:
                            break;
                        }

                        return false;
                    }
                });

                MenuInflater inflater = poMenu.getMenuInflater();
                inflater.inflate(R.menu.main, poMenu.getMenu());
                poMenu.show();
            }
        });
    }

1572856648(1)

posted @ 2019-11-04 16:39  thomas_blog  阅读(205)  评论(0编辑  收藏  举报