Android的UI組件復選框控件CheckBox
既然是復選框,當然具備選中跟未選中狀態,我們可以根據控件是否被選中來進行相應的操作,通過對復選框加載時間監聽器,來對控件狀態的改變作出Actions,當然也可以只對控件是否被選中做判斷即可,而在其他控件的監聽處理中執行其他操作,這個就根據具體的業務需求來選擇。
我們先在布局文件中聲明創建3個復選框控件,分別稱為“WWW”,“ATAAW”,“COM”。
<CheckBox android:text="WWW" android:id="@+id/ataaw1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
<CheckBox android:text="ATAAW" android:id="@+id/ataaw2"
android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
<CheckBox android:text="COM" android:id="@+id/ataaw3"
android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
為以上復選框添加時間監聽器,為了方便起見,我們這里為三個復選框添加同一個時間監聽器,通過判斷其響應的ID確定哪一個復選框被選中。
A、首先定義監聽器
OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.ataaw1: //action
break;
case R.id.ataaw2: //action
break;
case R.id.ataaw3: //action
break;
}
}
}
B、指定監聽器
CheckBox www = (CheckBox) this.findViewById(R.id.ataaw1);
CheckBox ataaw = (CheckBox) this.findViewById(R.id.ataaw2);
CheckBox com = (CheckBox) this.findViewById(R.id.ataaw3);
www.setOnCheckedChangeListener(listener);
ataaw.setOnCheckedChangeListener(listener);
com.setOnCheckedChangeListener(listener);
以上即是Android開發中復選框的基本使用方法。
?
鳳凰涅槃/浴火重生/馬不停蹄/只爭朝夕
???? 隱姓埋名/低調華麗/簡單生活/完美人生
posted on 2010-03-17 10:16 poetguo 閱讀(4967) 評論(0) 編輯 收藏 所屬分類: Android