如何在 Android 弹出菜单中设置默认选定选项? [英] How to set a default selected option in Android popup menu?
本文介绍了如何在 Android 弹出菜单中设置默认选定选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码创建菜单:
I am using following code to create a menu:
PopupMenu popup = new PopupMenu(getApplicationContext(), v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.equalizer, popup.getMenu());
popup.show();
均衡器.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="@+id/flat"
android:title="Flat" />
<item android:id="@+id/stadium"
android:title="Stadium" />
<item android:id="@+id/jazz"
android:title="Jazz" />
<item android:id="@+id/rock"
android:title="Rock" />
<item android:id="@+id/pop"
android:title="Pop" />
</group>
</menu>
如何将菜单的第三个选项设置为默认选中?
How is it possible to set, let's say, 3rd option of the menu as selected by default?
我想以编程方式更改默认值.
I would like to change defaults programmatically.
推荐答案
已给出在 xml 中设置选中项的答案.如果你想在代码中做到这一点,请使用:
Answers have been given for setting a checked item in xml. If you want to do it in code, use:
popup.getMenu().getItem(2).setChecked(true);
选择第三项.在你当然膨胀了菜单之后......
to select the 3rd item. After you inflated the menu of course...
这篇关于如何在 Android 弹出菜单中设置默认选定选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文