1、CheckBox的两种状态:

选中状态(true),未选中状态(false);

2、属性:

android:id="@+id/checkbox"

android:layout_width="warp_content"

android:layout_height="warp_content"

android:checked="false"

android:text="男"

3、应用:

拖入CheckBox控件,并对它进行相关设置:

<!--activity_main.xml-->    <CheckBox        android:checked="false"        android:id="@+id/checkBox1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="你是猪" />

初始化CheckBox,并设置监听器监听他的状态,并根据对应状态输出提示信息:

public class MainActivity extends Activity {//定义checkBoxprivate CheckBox checkBox;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //初始化checkBox        checkBox=(CheckBox) findViewById(R.id.checkBox1);                //通过设置checkBox的监听事件来对checkBox是不是被选中        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {            //通过匿名内部类来实现监听器@Overridepublic void onCheckedChanged(CompoundButton arg0, boolean arg1) {// TODO Auto-generated method stub//通过onCheckChanged来监听当前的checkBox是否被选中if(arg1){//获得checkBox的文本内容String text="对!"+checkBox.getText().toString();Toast.makeText(MainActivity.this, text, 1).show();}else{Toast.makeText(MainActivity.this, "不对,你是猪!", 1).show();}}});            } }

4、效果:

Android控件笔记——CheckBox复选框_第1张图片

Android控件笔记——CheckBox复选框_第2张图片

更多相关文章

  1. Android 布局----让一个控件居底部
  2. Android中控件的隐藏和显示
  3. 各种控件
  4. 用EditText控件的属性inputType
  5. Android Studio控件属性大全
  6. Android 控件xml属性详解
  7. Android:控件布局(相对布局)RelativeLayout内摆放
  8. ANDROID:控件属性(很全)
  9. android 使控件透明

随机推荐

  1. 【Android】自定义 Tabhost
  2. Android(安卓)SystemClock
  3. Android(安卓)basic1
  4. 怎样更新Android(安卓)SDK1.1 到 Android
  5. Android(安卓)HTTP框架Volley详解
  6. android studio3 多渠道打包及编译速度优
  7. Android(安卓)basic1
  8. Android平板获取唯一标识DeviceId
  9. Android(安卓)basic1
  10. Android(安卓)自带图标库 android.R.draw