gpt4 book ai didi

java - Android点击弹出菜单项不显示在弹出菜单上

转载 作者:太空宇宙 更新时间:2023-11-04 11:41:45 27 4
gpt4 key购买 nike

我单击操作栏上的图标以显示弹出菜单,然后单击弹出菜单项,Java 代码中的 toast 消息显示它将状态从 false 更改为 true。问题是,当再次打开弹出菜单时,没有单击任何项​​目。单击弹出菜单项始终会显示其状态从 false 更改为 true。

有人有解决这个问题的代码吗?

感谢您的帮助。

Java代码:

@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_action, menu);

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

if (id == R.id.action_popup) {

View menuItemView = findViewById(R.id.action_popup);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popupMenu.getMenu());

popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

public boolean onMenuItemClick(MenuItem item) {

int id = item.getItemId();

if (id == R.id.opt1_name) {

if (item.isChecked()){
item.setChecked(false);
Toast.makeText(getApplicationContext(), "Logic Set False= " + item, Toast.LENGTH_SHORT).show();
}
else{
item.setChecked(true);
Toast.makeText(getApplicationContext(), "Logic Set True= " + item, Toast.LENGTH_SHORT).show();
}


}

else if (id == R.id.opt2_date) {

if (item.isChecked()){item.setChecked(false);
Toast.makeText(getApplicationContext(), "Logic Set False= " + item, Toast.LENGTH_SHORT).show();
}
else{item.setChecked(true);
Toast.makeText(getApplicationContext(), "Logic Set True= " + item, Toast.LENGTH_SHORT).show();
}

}

return true;
}
});

popupMenu.show();
}

return super.onOptionsItemSelected(item);
}

menu_action.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=".PlayerActivity">

<item android:id="@+id/action_popup" android:title="Sort Popup" android:icon="@drawable/ic_sort"
android:showAsAction="always" />

<item android:id="@+id/action_settings" android:title="Settings"
app:showAsAction="never" />

<item android:id="@+id/action_help" android:title="Help"
app:showAsAction="never" />

</menu>

popup_menu.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/opt1_name"
android:title="Name" />

<item
android:id="@+id/opt2_date"
android:title="Date" />
</group>

</menu>

最佳答案

检查错误,如果您检查,那么您的 mainlist.xml 文件中必须有一个 id 为 @android:id/list 的 ListView

<ListView
android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_height="fill_parent"/>

关于java - Android点击弹出菜单项不显示在弹出菜单上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42724644/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com