学习啦 > 知识大全 > 知识百科 > 公共基础知识 > 安卓checkbox的用法Android平台优势

安卓checkbox的用法Android平台优势

时间: 谢君787 分享

安卓checkbox的用法Android平台优势

  安卓开发中经常会用到checkbox,那么具体是如何使用的呢?以下是由学习啦小编整理关于安卓checkbox的用法的内容,希望大家喜欢!

  安卓checkbox的用法

  CheckBox定义一个同意协议的按钮,只要同意button才可以点击

  XML代码

  <CheckBox

  android:id="@+id/checkbox1"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_above="@+id/button1"

  android:layout_alignLeft="@+id/linearLayout1"

  android:text="牛仔"

  />

  在onClick里面设置只要当checkbox.isChecked()为true,也就是勾选上时,button1.setEnabled(true);才可以点击

  java代码

  checkbox = (CheckBox) findViewById(R.id.checkbox1);

  checkbox.setChecked(false);

  button1.setEnabled(false);

  checkbox.setOnClickListener(new CheckBox.OnClickListener(){

  <span style="white-space:pre"> </span>@Override

  public void onClick(View v) {

  // TODO Auto-generated method stub

  if(checkbox.isChecked()){

  button1.setEnabled(true);

  }else{

  <span style="white-space:pre"> </span>button1.setEnabled(false);

  }

  <span style="white-space:pre"> </span>}

  });

  定义多个CheckBox来控制同一个控件

  XML代码

  <CheckBox

  android:id="@+id/checkbox1"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_above="@+id/button1"

  android:layout_alignLeft="@+id/linearLayout1"

  android:text="牛仔"

  />

  <CheckBox

  android:id="@+id/checkbox2"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_alignBaseline="@+id/checkbox3"

  android:layout_alignBottom="@+id/checkbox3"

  android:layout_marginLeft="27dp"

  android:layout_toRightOf="@+id/checkbox3"

  android:text="面包" />

  <CheckBox

  android:id="@+id/checkbox3"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_alignBaseline="@+id/checkbox1"

  android:layout_alignBottom="@+id/checkbox1"

  android:layout_toRightOf="@+id/button1"

  android:text="黄油" />

1697483