弹出菜单上未显示Android弹出菜单项的弹出式菜单项

时间:2017-03-10 17:45:11

标签: java android android-actionbar popupmenu

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

1 个答案:

答案 0 :(得分:0)

检查错误,如果您检查,那么您的mainlist.xml文件中必须有一个listview,其id为<ListView android:id="@android:id/list" android:layout_height="wrap_content" android:layout_height="fill_parent"/>

//---------------------------------------------------------------------------
// OnShowUI
//
// This function drives the UI sequence and file transfer of the setup.
// 
// The OnShowUI event is called directly by the framework to initiate
// the UI sequence and file transfer of the setup. By default this event
// displays UI that informs the end user that the maintenance setup has been
// completed successfully.
//---------------------------------------------------------------------------
function OnShowUI()
    BOOL bMaintenanceMode, bUpdateMode;
    string szIgnore, szTitle;
    LIST listDirs;
    number nFindAllDirsResult, nFindAllFilesResult;
    BOOL lDirEmpty;

begin
    // Enable dialog caching
    Enable( DIALOGCACHE );

    // Determine what events to show
    bUpdateMode = FALSE;
    bMaintenanceMode = FALSE;

    // Remove this to disabled update mode
    if (UPDATEMODE) then
        // checking to make sure app still exists in orig location
        if Is(PATH_EXISTS, TARGETDIR) then
            // Also check for empty TargetDir
            lDirEmpty = IsTargetDirEmpty();

            if (lDirEmpty) then
                // TARGETDIR is completely empty, so disable UPDATE mode
                bUpdateMode = FALSE;
            else
                // TARGETDIR has some contents, so continue with UPDATE
                bUpdateMode = TRUE;
            endif;
        else
            // Turn off Update mode if original folder is gone
            bUpdateMode = FALSE;
        endif;

        if (!bUpdateMode) then
            // If Update mode is set but the original target is missing
            // need to flag the installer to force full reinstall (otherwise it will 
            // think all features have already been installed (by analyzing the log))
            FeatureReinstall();
        endif;
    endif;

    // Remove this to disable maintenance mode.
    if (MAINTENANCE) then
        // checking to make sure app still exists in orig location
        if Is(PATH_EXISTS, TARGETDIR) then
            // Also check for empty TargetDir
            lDirEmpty = IsTargetDirEmpty();

            if (lDirEmpty) then
                // TARGETDIR is completely empty, so disable Maint mode
                bMaintenanceMode = FALSE;
            else
                // TARGETDIR has some contents, so continue with Maint
                bMaintenanceMode = TRUE;
            endif;
        else
            // Turn off maintenance mode if original folder is gone
            bMaintenanceMode = FALSE;
        endif;

        if (!bMaintenanceMode) then
            // If Maintenance mode is set but the original target is missing
            // need to flag the installer to force full reinstall (otherwise it will
            // think all features have already been installed (by analyzing the log))
            FeatureReinstall();
        endif;
    endif;

    // Show appropriate UI

    if( bUpdateMode ) then
        OnUpdateUIBefore();
    else
        if ( bMaintenanceMode ) then
            OnMaintUIBefore();
        else
            OnFirstUIBefore();
        endif;
    endif;

    // Move Data
    OnMoveData();

    if( bUpdateMode ) then
        OnUpdateUIAfter();
    else
        if ( bMaintenanceMode ) then
            OnMaintUIAfter();
        else
            OnFirstUIAfter();
        endif;
    endif;

    // Disable dialog caching
    Disable(DIALOGCACHE);
end;