如何拥有2个OptionsItemSelected?

时间:2017-01-03 08:03:11

标签: android menu popup

在我的活动中,我在action bar

中有两个图标按钮

enter image description here

这是add_task.xml

<item
    android:id="@+id/action_add_task"
    android:icon="@drawable/create_new"
    android:title="Add New"
    app:showAsAction="always" />

<item
    android:id="@+id/menu"
    android:icon="@drawable/menu"
    android:title="Menu"
    app:showAsAction="always" />

点击图标menu后,我就明白了

enter image description here

popup.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/opt1"
    android:icon="@drawable/change_pic"
    android:title="Change Profile" />

    <item
        android:id="@+id/opt2"
        android:icon="@drawable/sign_out"
        android:title="Sign Out" />
</menu>

我的问题是,点击Toast图标时会显示menu。我希望它仅在单击更改配置文件时显示。

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.add_task, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case R.id.menu:
                View menuItemView = findViewById(R.id.menu);
                MenuBuilder menuBuilder =new MenuBuilder(this);
                MenuInflater inflater = new MenuInflater(this);
                inflater.inflate(R.menu.popup, menuBuilder);
                MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, menuItemView);
                optionsMenu.setForceShowIcon(true);
                optionsMenu.show();

                case R.id.opt1: // when change profile clicked
           Toast.makeText(getApplication(),"Profile",Toast.LENGTH_SHORT).show();


            default:
                return super.onOptionsItemSelected(item);
        }
    }

修改

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.menu:
                View menuItemView = findViewById(R.id.menu);
                MenuBuilder menuBuilder = new MenuBuilder(this);
                MenuInflater inflater = new MenuInflater(this);
                inflater.inflate(R.menu.popup, menuBuilder);
                final MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, menuItemView);
                opionsMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        //  Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                        if ("Change Profile".equals(item.getTitle())) {
                            Intent intent = new Intent(AddMonthlyExpenses.this, Profile.class);  // go to Information class
                            intent.putExtra("name", name);
                            startActivity(intent);

                        }
                        optionsMenu.setForceShowIcon(true);
                        optionsMenu.show();
                        return true;
                    }
                });

            default:
                return super.onOptionsItemSelected(item);

        }

        return true;
    }

我在这一行得到错误

 opionsMenu.setOnMenuItemClickListener

optionsMenu无法解决。

2 个答案:

答案 0 :(得分:0)

您尚未添加Break语句

 @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    super.onOptionsItemSelected(item);

    switch(item.getItemId()){
        case R.id.menu:
                View menuItemView = findViewById(R.id.menu);
                MenuBuilder menuBuilder =new MenuBuilder(this);
                MenuInflater inflater = new MenuInflater(this);
                inflater.inflate(R.menu.popup, menuBuilder);
                MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, menuItemView);
                optionsMenu.setForceShowIcon(true);
                optionsMenu.show();
                break;

    case R.id.opt1: // when change profile clicked
           Toast.makeText(getApplication(),"Profile",Toast.LENGTH_SHORT).show();
                break;

    }
    return super.onOptionsItemSelected(item);

}

试试这段代码

答案 1 :(得分:0)

最后我找到了答案

            case R.id.menu:
            View menuItemView = findViewById(R.id.menu);
            MenuBuilder menuBuilder = new MenuBuilder(this);
            MenuInflater inflater = new MenuInflater(this);
            inflater.inflate(R.menu.popup, menuBuilder);
            MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, menuItemView);
            optionsMenu.setForceShowIcon(true);

            // Set Item Click Listener
            menuBuilder.setCallback(new MenuBuilder.Callback() {
                @Override
                public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.opt1: // Handle option1 Click
                            Intent intent = new Intent(AddMonthlyExpenses.this,Profile.class);
                            startActivity(intent);
                            return true;
                        case R.id.opt2: // Handle option2 Click
                            return true;
                        default:
                            return false;
                    }
                }

                @Override
                public void onMenuModeChange(MenuBuilder menu) {}
            });


            // Display the menu
            optionsMenu.show();