[英]Android How to open the options menu item after clicking a button?
我正在使用这样的菜单项。
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_overflow"
android:enabled="true"
android:icon="@drawable/ic_menu_moreoverflow_mtrl_alpha"
android:title=""
app:showAsAction="ifRoom">
<menu>
<item
android:id="@+id/action_city"
android:icon="@drawable/ic_change_city"
android:title="Change City"
app:showAsAction="never" />
<item
android:id="@+id/action_language"
android:icon="@drawable/ic_change_language"
android:title="Change Language"
app:showAsAction="never" />
<item
android:id="@+id/action_change_theme"
android:icon="@drawable/ic_change_theme"
android:title="Change Theme"
app:showAsAction="never" />
</menu>
</item>
</menu>
现在如何在单击按钮后以编程方式展开 Menu Item -> menu_overflow
?
我的代码是:
activity.openOptionsMenu();
---------
toolbar.showOverflowMenu();
---------
menu_overflow.expandActionView();
---------
menu_overflow.getSubMenu().getItem().expandActionView();
但是上面的那些代码不起作用。
Create one xml file names as menu_main. xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="example.javatpoint.com.optionmenu.MainActivity">
<item android:id="@+id/item1"
android:title="Item 1"
android:icon="@drawable/ic_action_accept" />
<item android:id="@+id/item2"
android:title="Item 2"/>
<item android:id="@+id/item3"
android:title="Item 3"
app:showAsAction="withText"/>
</menu>
Then add these code in your Activity :
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.item1:
Toast.makeText(getApplicationContext(),"Item
Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item2:
Toast.makeText(getApplicationContext(),"Item 2
Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item3:
Toast.makeText(getApplicationContext(),"Item 3
Selected",Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
尝试这个
YourActivity.this.openOptionsMenu();
使用 Activity.openOptionsMenu(),但您可能需要延迟调用它。 像下面
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
openOptionsMenu();
}
}, 1000);
如果需要自动显示子菜单。 您可以在 openOptionsMenu 之后调用 menu.performIdentifierAction,如下所示
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
openOptionsMenu();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mainMenu.performIdentifierAction(R.id.submenu, 0);
}
}, 500);
}
}, 1000);
Android 开发效率工具: https://play.google.com/store/apps/details?id=cn.trinea.android.developertools
Android 开源项目: https://p.codekk.com/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.