简体   繁体   English

检查菜单组中的默认项目

[英]Checking Default Item In Menu Group

I am trying to check a default item in a menu group. 我正在尝试检查菜单组中的默认项目。 I am not changing anything in the menu using code. 我没有使用代码更改菜单中的任何内容。

The menu is as follows: 菜单如下:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item android:title="Map type">
    <menu>
        <group
            android:checkableBehavior="single"
            >
            <item
                android:id="@+id/nav_map_type_normal"
                android:title="Normal"
                android:checked="true"
                />
            <item
                android:id="@+id/nav_map_type_satellite"
                android:title="Satellite"
                />
            <item
                android:id="@+id/nav_map_type_terrain"
                android:title="Terrain"
                />
            <item
                android:id="@+id/nav_map_type_hybrid"
                android:title="Hybrid"
                />
        </group>
    </menu>
</item>

<item
    android:id="@+id/nav_toggle_traffic"
    android:title="Toggle traffic"
    />

<item
    android:id="@+id/nav_settings"
    android:icon="@drawable/ic_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100"
    />

</menu>

When the menu is first displayed, the first item (Normal) is highlighted to indicate it is checked. 首次显示菜单时,第一个项目(正常)将突出显示以指示已选中。 This is what I want to happen. 这就是我想要发生的事情。 I then want the highlighting to move through the group as each item is pressed. 然后,我希望突出显示在按下每个项目时在组中移动。

The problem is: 问题是:

  • If I press Satellite, BOTH Normal and Satellite are highlighted 如果我按下卫星,则突出显示正常和卫星

    If I then press Hybrid, Normal remains highlighted , Hybrid is highlighted and Satellite reverts to its unchecked state. 如果我按下Hybrid, Normal仍然会突出显示 ,Hybrid会突出显示,Satellite会恢复到未选中状态。

Only after actually pressing Normal does its checked state behave as it should. 只有在实际按下Normal后,其检查状态才会按预期运行。

I have tried removing the android:checked="true" and using performIdentifierAction in code, but this did not change the checked state. 我已经尝试删除android:checked =“true”并在代码中使用performIdentifierAction,但这并没有改变检查状态。

Since you are making NORMAL as default TRUE, you need explicitly SET it to false as and when Satellite, Hybrid or Terrain is selected. 由于您将NORMAL设置为默认值TRUE,因此当选择Satellite,Hybrid或Terrain时,您需要将其显式设置为false。

I believe currently, you would be setting NORMAL as android:checked=false on the onclickmenu selection code of nav_map_type_normal. 我相信目前,你会在nav_map_type_normal的onclickmenu选择代码上将NORMAL设置为android:checked = false。 ie you are basically un-checking it as by default it is selected as TRUE. 即你基本上没有检查它,因为默认它被选为TRUE。 Please do the same when other options are selected. 选择其他选项时请执行相同操作。

Sorry, I forgot to mention that the menu is used in a NavigationView and as it it turns out that changes everything. 对不起,我忘了提到菜单在NavigationView中使用,结果发现它改变了一切。

navigationView.setCheckedItem(R.id.nav_map_type_normal);

performs exactly the way I want. 完全按我想要的方式表现。 I found it in a comment here Navigation drawer: How do I set the selected item at startup? 我在评论中找到了它导航抽屉:如何在启动时设置所选项目?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM