Android——Check box of basic control: CheckBox (8)

1.Knowledge points

(1) Master the use of CheckBox components.

2.Specific content

 

 Example: Let users choose interests and hobbies

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/myinit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选择兴趣爱好" />
	<CheckBox 
	    android:id="@+id/inti1"
	    android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打篮球"
	    />
	<CheckBox 
	    android:id="@+id/inti2"
	    android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="唱歌"
	    />
	<CheckBox 
	    android:id="@+id/inti3"
	    android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳舞"
	    />
	<CheckBox 
	    android:id="@+id/inti4"
	    android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="散打"
	    />
</LinearLayout>

Now the displayed effect is there, but there is no default selection. Now we can set it through the layout file or Activity program.

<CheckBox 
	    android:id="@+id/inti2"
	    android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="唱歌"
        android:checked="true"—设置默认选中
	    />

Now we set it up through the program.

public class CheckBoxActivity extends Activity {
	private CheckBox init3 = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkbox_check_box);
        this.init3 = (CheckBox) super.findViewById(R.id.inti3);
        this.init3.setChecked(true);//默认选中
    }
}

3. Summary

CheckBox component is used to implement the function of check box.

Guess you like

Origin blog.csdn.net/weixin_41830242/article/details/131205259