boolean[] isChecked;
private PopupMenu mPopupMenu;
private void showPopupMenu() {
// Button used to anchor the popup menu and to show it on its click
final Button button = (Button) findViewById(R.id.button);
if (mPopupMenu == null) {
//Creating the instance of PopupMenu
mPopupMenu = new PopupMenu(MainActivity.this, button);
//Inflating the Popup using xml file
mPopupMenu.getMenuInflater().inflate(R.menu.poupup_menu, mPopupMenu.getMenu());
isChecked = new boolean[mPopupMenu.getMenu().size()];
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPopupMenu.show();
mPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
int position = -1;
if (item.getItemId() == R.id.one) position = 0;
else if (item.getItemId() == R.id.two) position = 1;
else if (item.getItemId() == R.id.three) position = 2;
if (position != -1) {
isChecked[position] = !isChecked[position];
item.setChecked(isChecked[position]);
}
return true;
}
});
}
});
}
1条答案
按热度按时间uqxowvwt1#
您可以在中创建可检查菜单
res\menu
通过将项目 Package 成<group>
贴上android:checkableBehavior="all"
属性:poupup_menu.xml:
然后以编程方式将其膨胀,并在数组中跟踪选中的项: