简体   繁体   English

单击按钮打开菜单

[英]Open Menu from a button click

I have this code for showing a Menu: 我有这个代码用于显示菜单:

public boolean onCreateOptionsMenu(Menu menu) {
    this.menu = menu;
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.Email_settings:
 //    startActivity(new Intent(this, About.class));
    return true;
    case R.id.Help_settings:
//     startActivity(new Intent(this, Help.class));
    return true;
    default:
    return super.onOptionsItemSelected(item);
    }
}

The code works fine when pressing the device options button. 按下设备选项按钮时,代码工作正常。

How can i open the menu on a button click from inside my screen and not the device options button? 如何在屏幕内点击按钮而不是设备选项按钮上打开菜单?

call 呼叫

openOptionsMenu();

on Click event 点击事件

Use Activity.openOptionsMenu() method. 使用Activity.openOptionsMenu()方法。

Docs reference 文件参考

Be sure to use this method only after options menu is instanciated(onCreateOptionsMenu was already executed) 只有在实例化选项菜单后才能使用此方法(onCreateOptionsMenu已经执行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM