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;
- }
- };