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="黄油" />
Java代码
- checkbox = (CheckBox) findViewById(R.id.checkbox1);
- checkbox2 = (CheckBox) findViewById(R.id.checkbox2);
- checkbox3 = (CheckBox) findViewById(R.id.checkbox3);
- //通过OnCheckedChangeListener来设置来个CheckBox对象
- checkbox.setOnCheckedChangeListener(checkboxlister);
- checkbox2.setOnCheckedChangeListener(checkboxlister);
- checkbox3.setOnCheckedChangeListener(checkboxlister);
- }
- private CheckBox.OnCheckedChangeListener checkboxlister = new CheckBox.OnCheckedChangeListener(){
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- // TODO Auto-generated method stub
- String str0 = "所选:";
- String str1 = "牛仔";
- String str2 = "面包";
- String str3 = "黄油";
- //在这里进行你需要的逻辑
- if(checkbox.isChecked()){
- tview.setText(str0+str1);
- }
- if(checkbox2.isChecked()){
- tview.setText(str0+str2);
- }
- if(checkbox3.isChecked()){
- tview.setText(str0+str3);
- }
- }
- };
也可以使用OnTouchListener(触摸事件)来触发
- checkbox.setOnTouchListener(checktouch);
- checkbox2.setOnTouchListener(checktouch);
- checkbox3.setOnTouchListener(checktouch);
- }
- private CheckBox.OnTouchListener checktouch = new CheckBox.OnTouchListener(){
- @Override
- public boolean onTouch(View arg0, MotionEvent arg1) {
- // TODO Auto-generated method stub
- if(checkbox.isChecked()){
- tview.setText("mimi");
- }else{
- tview.setText("pipi");
- }
- return false;
- }
- };