Xml代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CkeckBoxActivity">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你会哪些移动开发:"
android:textSize="30dp"
android:textColor="#000000"
/>
<CheckBox
android:id="@+id/cb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="安卓"
android:textSize="25dp"
android:layout_below="@+id/tv_title"
android:layout_marginTop="10dp"
/>
<CheckBox
android:id="@+id/cb_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ios"
android:textSize="25dp"
android:layout_below="@+id/cb_1"
android:layout_marginTop="10dp"
/>
<CheckBox
android:id="@+id/cb_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="H5"
android:textSize="25dp"
android:layout_below="@+id/cb_2"
android:layout_marginTop="10dp"
/>
</RelativeLayout>
效果展示:
添加监听事件
Activity代码
private CheckBox mcb1;
private CheckBox mcb2;
private CheckBox mcb3;
mcb1=(CheckBox)findViewById(R.id.cb_1);
mcb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //设置是否选中的监听事件
Toast.makeText(CkeckBoxActivity.this,isChecked?"选中":"未选中",Toast.LENGTH_SHORT).show();
}
});
mcb2=(CheckBox)findViewById(R.id.cb_2);
mcb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(CkeckBoxActivity.this,isChecked?"选中":"未选中",Toast.LENGTH_SHORT).show();
}
});
}
效果展示: