【问题标题】:Android Clicks On Popup Menu Items Are Not Shown On The Popup MenuAndroid点击弹出菜单项未显示在弹出菜单上
【发布时间】:2017-03-11 02:23:54
【问题描述】:

我单击操作栏上的图标以显示弹出菜单,然后单击弹出菜单项,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>

【问题讨论】:

标签: java android android-actionbar popupmenu


【解决方案1】:

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

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

【讨论】:

  • 请忽略Logcat输出,发错帖,与本帖无关。该应用程序未在 Logcat 中显示任何输出。
猜你喜欢
  • 2013-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-11
相关资源
最近更新 更多