如何以编程方式打开选项菜单?

时间:2010-06-28 14:40:08

标签: android button menu android-activity

我想以编程方式打开optionsMenu,而无需单击用户的菜单键。我该怎么做?

11 个答案:

答案 0 :(得分:91)

或者只是致电Activity.openOptionsMenu()

答案 1 :(得分:31)

显然,在onCreate中执行此操作会破坏app,因为Activity尚未附加到窗口。如果你这样做:

@Override
public void onAttachedToWindow() {
    openOptionsMenu(); 
};

......它有效。

答案 2 :(得分:16)

对于使用 <tr ng-repeat="fixture in fixtures.fixtures "> <td>{{fixture.date | cmdate:'MM/dd/yyyy'}}</td> <td>{{teamName(fixture.homeTeam) }}</td> <td>&nbsp;{{score(fixture.goalsHomeTeam)}}</td> <td>-</td> <td>{{score(fixture.goalsAwayTeam)}}&nbsp;</td> <td>{{teamName(fixture.awayTeam)}}</td> </tr> 的新Toolbar类的开发人员,这就是它的完成方式:

Support Library

答案 3 :(得分:11)

将这行代码放在onResume()中,这应该有帮助!

new Handler().postDelayed(new Runnable() { 
   public void run() { 
     openOptionsMenu(); 
   } 
}, 1000); 

答案 4 :(得分:8)

来自名为MainActivity的活动内的OnClickListener:

MainActivity.this.openOptionsMenu();

答案 5 :(得分:5)

如果使用AppCompatActivity

public class Sorting {

public static void sort(Comparable[] a) {
    int N = a.length;
    for (int i = 1; i < N; i++) {                            
        for (int j = i; j > 0 && less(a[j], a[j - 1]); j--)
            swap(a, j, j - 1);
    }
}

private static void swap(Comparable[] a, int i, int j) { 
    Comparable t = a[i];
    a[i] = a[j];
    a[j] = t;
}

private static boolean less(Comparable v, Comparable w) {                                                   
    return v.compareTo(w) < 0;
}

答案 6 :(得分:1)

两种方法:

Activity.getWindow().openPanel(Window.FEATURE_OPTIONS_PANEL, event);

event参数是KeyEvent,用于描述用于打开菜单的键,可以根据键盘的类型修改菜单的显示方式。

或者......通过模拟用户按下按钮:

IWindowManager wManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
KeyEvent kd = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SOFT_LEFT);
KeyEvent ku = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_SOFT_LEFT);
wManager.injectKeyEvent(kd.isDown(), kd.getKeyCode(), kd.getRepeatCount(), kd.getDownTime(), kd.getEventTime(), true);

答案 7 :(得分:0)

如果您在视图中,可以编写

    ((Activity)getContext()).openOptionsMenu();

答案 8 :(得分:0)

经过长时间的研究和多次尝试,唯一的方法似乎是模拟KeyEvent。这使选项菜单出现:

BaseInputConnection mInputConnection = new BaseInputConnection( findViewById(R.id.main_content), true);
KeyEvent kd = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU);
KeyEvent ku = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU);
mInputConnection.sendKeyEvent(kd);
mInputConnection.sendKeyEvent(ku);

答案 9 :(得分:0)

对我来说,在onClick中声明toolbar.showOverflowMenu()是行不通的。 openOptionsMenu()也不适合我。而不是以下方式对我有效,

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                toolbar.showOverflowMenu();
            }
        }, 500);

答案 10 :(得分:0)

toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitleTextColor(0xFFFFFFFF);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            toolbar.showOverflowMenu();
        }
    }, 100);