Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

android - Detect click on Actionbar's Overflow menu button

Can I detect click/tap on the menu button of action bar, i.e. used to show overflow menu items?

By default it opens up the list with one item "Settings". Here is the screenshot:

Until now it is detecting click on "2" but I want to detect click on "1".

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To detect the click on the overflow menu have such code:

@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) {
    ...
}

To detect click on menu items, in case you have a menu like that :

<?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>

You should be able to detect the click in

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...