从MenuItem获取Id并转到下一个活动

时间:2018-06-19 08:35:45

标签: android android-menu

我创建了一个Menu,其中包含menuitems。我需要在menuitems后检索pressed ID,然后必须进行检查才能看到ID包含的内容已经pressed并转到新的Activity。我尝试过使用item.getTitle()item.getItemId(),但它没有进入if statement

popup_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/one"
    android:title="TOOL 7 - THE FORCE"/>

<item
    android:id="@+id/two"
    android:title="TOOL 5 - FOCUS"/>

<item
    android:id="@+id/three"
    android:title="TOOL 3 - ACTION"/>

<item
    android:id="@+id/four"
    android:title="TOOL 9 - QUESTION FIX"/>

</menu>

检索MenuItemId的代码

 btnToolbox = (Button) findViewById(R.id.btnToolbox);
    btnToolbox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popup = new PopupMenu(Screen2_11.this,btnToolbox);
            popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());

            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    itemName = String.valueOf(item.getTitle());
                    itemId = item.getItemId();
                    if ( itemName == "TOOL 7 - THE FORCE" || itemId == "one" ){
                        Intent i = new Intent(getApplicationContext(), Screen108_Force.class);
                        startActivity(i);
                    }
                    else if ( itemName == "TOOL 5 - FOCUS" || itemId == "two" ) {
                        Intent i = new Intent(getApplicationContext(), Screen120_Focus.class);
                        startActivity(i);
                    }
                    else if ( itemName == "TOOL 3 - ACTION" || itemId == "three" ) {
                        Intent i = new Intent(getApplicationContext(), Screen119_Action.class);
                        startActivity(i);
                    }
                    else if ( itemName == "TOOL 9 - QUESTION FIX" || itemId == "four" ) {
                        Intent i = new Intent(getApplicationContext(), Screen136_Questions.class);
                        startActivity(i);
                    }
                    return true;
                }
            });
            popup.show();
        }
    });

2 个答案:

答案 0 :(得分:1)

使用此 if (itemId == R.id.one) 代替if ( itemName == "TOOL 7 - THE FORCE" || itemId == "one" )

试试这个

    btnToolbox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popup = new PopupMenu(Screen2_11.this, btnToolbox);
            popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());

            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {

                   int itemId = item.getItemId();

                    if (itemId == R.id.one) {
                        Intent i = new Intent(getApplicationContext(), Screen108_Force.class);
                        startActivity(i);
                    } else if (itemId == R.id.two) {
                        Intent i = new Intent(getApplicationContext(), Screen120_Focus.class);
                        startActivity(i);
                    } else if (itemId == R.id.three) {
                        Intent i = new Intent(getApplicationContext(), Screen119_Action.class);
                        startActivity(i);
                    } else if (itemId == R.id.four) {
                        Intent i = new Intent(getApplicationContext(), Screen136_Questions.class);
                        startActivity(i);
                    }
                    return true;
                }
            });
            popup.show();
        }
    });

答案 1 :(得分:0)

您需要先初始化菜单。完成onCreate()

后添加此部分
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu_main,menu);
    return true;
}

然后在此处理项目按下选项。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //here initialize your button pressed.

    return super.onOptionsItemSelected(item);
}