Android-如何将默认选中的复选框添加到导航抽屉 [英] Android - How to add a default checked CheckBox to Navigation Drawer

查看:118
本文介绍了Android-如何将默认选中的复选框添加到导航抽屉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在导航抽屉中添加一个复选框,并且我希望其中一个为default checked

I want to add a check box to Navigation Drawer, and I want one of them to be default checked

我使用的是:app:actionViewClass="android.widget.Switch",如此答案中所建议.

I'm using this: app:actionViewClass="android.widget.Switch" as suggested in this answer.

但是我不知道如何对其中之一进行默认检查.如果我使用此属性android:checked="true",则会选中该选项而不是复选框(参见图片).

But I couldn't figure out how to make one of them default checked. If I use this property android:checked="true", then the option is checked instead of checkbox (See image).

有人知道如何将其默认选中吗(如果可能的话,我只想使用XML进行检查)?

Does anyone know how to make it default checked (if possible, I want to do it in XML only)?

这是我的 activity_drawer.xml .

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

    <group
        android:checkableBehavior="single">
        <item
            android:id="@+id/checkboxX-axis"
            android:title="Show x-axis"
            android:icon="@drawable/ic_x_axis_black_24dp"
            app:actionViewClass="android.widget.CheckBox"
            android:checked="true"
            />
        <item
            android:id="@+id/checkboxY-axis"
            android:title="Show y-axis"
            android:icon="@drawable/ic_y_axis_black_24dp"
            app:actionViewClass="android.widget.CheckBox"
            />
        <item
            android:id="@+id/checkboxZ-axis"
            android:title="Show z-axis"
            android:icon="@drawable/ic_z_axis_black_24dp"
            app:actionViewClass="android.widget.CheckBox"
            />
    </group>
</menu>

推荐答案

您可以执行以下操作:

MenuItem item = navigation.getMenu().findItem(R.id.checkboxX-axis);
CompoundButton compoundButton = (CompoundButton) item.getActionView();
compoundButton.setChecked(true);

NavigationView的名称替换navigation.

这篇关于Android-如何将默认选中的复选框添加到导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆