Android:勾选复选框

时间:2013-10-23 10:11:47

标签: java android checkbox

在我的Android应用程序中,我想定义一个应标记为选中的复选框。任何人都可以解释如何做到这一点吗?

提前完成

4 个答案:

答案 0 :(得分:7)

您可以在xml中定义如下:

<CheckBox 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your text"
        android:checked="true"
        />

如果您想在java中获得相同的内容,可以使用setChecked(boolean)的{​​{1}}属性。

像,

checkbox

答案 1 :(得分:0)

使用setChecked(boolean)方法。这是一个来自CompoundButtons的方法,CheckBox扩展了。请参阅文档here

答案 2 :(得分:0)

尝试这个,

public void setSelected(boolean selected)

答案 3 :(得分:0)

<强> XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/chkIos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_ios" />

    <CheckBox
        android:id="@+id/chkAndroid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_android"
        android:checked="true" />

    <CheckBox
        android:id="@+id/chkWindows"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_windows" />

    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />

</LinearLayout>

默认选中Make CheckBox: 将android:checked =“true”放在复选框元素中,使其默认检查。在这种情况下,默认情况下会选中“Android”选项。