6

我可以检测到点击/点击操作栏的菜单按钮,即用于显示溢出菜单项吗?

默认情况下,它会打开包含一项“设置”的列表。这是屏幕截图:

到目前为止,它正在检测点击“2”,但我想检测点击“1”。

4

4 回答 4

14

要检测对溢出菜单的点击有这样的代码:

@Override
public boolean onMenuOpened(int featureId, Menu menu) {
    if(featureId == AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR && menu != null){
        //overflow menu clicked, put code here...
    }
    return super.onMenuOpened(featureId, menu);
}

@Override
public void onPanelClosed(int featureId, Menu menu) {
    ...
}

要检测对菜单项的点击,以防您有这样的菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu2" android:alphabeticShortcut="b"
        android:title="Menu No. 2" android:orderInCategory="2">
        <menu >
        <group android:id="@+id/group2" android:checkableBehavior="single">
            <item android:id="@+id/submenu1" android:title="SubMenu No. 1" />
            <item android:id="@+id/submenu2" android:title="SubMenu No. 2" />
        </group>  
        </menu>
    </item>
</menu>

您应该能够检测到点击

onOptionsItemSelected

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    Log.w("ANDROID MENU TUTORIAL:", "onOptionsItemSelected(MenuItem item)");

    // Handle item selection
    switch (item.getItemId()) {
    case R.id.menu2:
        Toast.makeText(this, "Clicked: Menu No. 2", Toast.LENGTH_SHORT).show();
        return true;   
        ...
}
于 2014-08-12T08:09:08.563 回答
1

最后我找到了解决方案。覆盖 FragmentActivity.onKeyDown

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    switch (keyCode) {

    case KeyEvent.KEYCODE_MENU:
        // Do Sometihng
        break;

    default:
        break;
    }
    return super.onKeyDown(keyCode, event);
} 
于 2014-08-12T09:46:17.357 回答
0

默认情况下,点击溢出按钮会显示选项菜单,所以我相信你应该设法拦截事件,然后通过覆盖做你想做的事Activity.onPrepareOptionsMenu

这有点棘手,因为我找不到溢出按钮的 id,所以我使用了这个 hack。在你的Activity

private boolean actionBarClicked = false;

@Override
public boolean onOptionsItemSelected (MenuItem item) {
    if (item.getId() == )
        actionBarClicked = true;
    return false; // Let default processing occur
}

@Override
public boolean onPrepareOptionsMenu (Menu menu) {
    if (actionBarClicked) {
        // Overflow button of ActionBar was clicked, do what you want here.
        actionBarClicked = false;
    }
    ...
}
于 2014-08-12T07:57:04.587 回答
0

如果您正确设置了工具栏和膨胀菜单,只需在MainActivity

@Override
public boolean onMenuOpened(int featureId, Menu menu) {
    //Perform some action on menu open
    return super.onMenuOpened(featureId, menu);
}

@Override
public void onPanelClosed(int featureId, Menu menu) {
    //Perform some action on menu closed
}

设置工具栏的onCreate功能MainActivity

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

并膨胀您在 xml 文件中定义的菜单

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

my_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_something"
        android:title="@string/something_text" />
    <!--Other menu items-->
</menu>
于 2018-08-04T11:38:00.290 回答