1.知识点
(1)掌握CheckBox组件的使用。
2.具体内容
范例:让用户选择兴趣爱好
<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>
现在显示的效果已经有了,但是没有默认的选中项,现在我们可以通过布局文件或者Activity程序去设置。
<CheckBox
android:id="@+id/inti2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="唱歌"
android:checked="true"—设置默认选中
/>
现在我们通过程序去设置。
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.小结
CheckBox组件用于实现复选框的功能。