简体   繁体   中英

Android : mark as checked a check box

In my android app, I want to define a check box which should be marked as checked. Can anyone plz explain how to do it ?

Thanx in advance

You can define in xml like the following:

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

If you want to attain the same in java, you can use setChecked(boolean) attribute of checkbox .

Like,

checkbox.setChecked(true);

Use the setChecked(boolean) method. It is a method from CompoundButtons, which CheckBox extends. See docs here .

try this one,

public void setSelected (boolean selected)

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 is checked by default : Put android:checked="true" inside checkbox element to make it checked bu default. In this case, “Android” option is checked by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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