單選按鈕radio例子:
RadioButton應該定義在RadioGroup中,組中只能選一個
跟html中
<input type="radio" name="gender" value="female"/>女
<input type="radio" name="gender" value="male"/>男
是一樣的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"
/>
<RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"
/>
</RadioGroup>
</LinearLayout>
取得Radio的值例子:
public class RadioActivity extends Activity {
private RadioButton femaleButton;
private RadioButton maleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio);
RadioGroup genderGroup=(RadioGroup) findViewById(R.id.genderGroup);
femaleButton=(RadioButton) findViewById(R.id.femaleButton);
maleButton=(RadioButton) findViewById(R.id.maleButton);
//給RadioGroup添加一個監聽器,監聽值的變化
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==femaleButton.getId()){
Toast.makeText(getApplicationContext(), "female", 1).show();
}else if(checkedId==maleButton.getId()){
Toast.makeText(getApplicationContext(), "male", 1).show();
}
}
});
}
}
<CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swim"
/>
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/run"
/>
<CheckBox
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read"
/>
得到複選按鈕選中的值
public class CheckBoxActivity extends Activity {
private CheckBox swimBox;
private CheckBox runBox;
private CheckBox readBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio);
swimBox=(CheckBox) findViewById(R.id.swim);
runBox=(CheckBox) findViewById(R.id.run);
readBox=(CheckBox) findViewById(R.id.read);
//給各個CheckBox 設置監聽器
swimBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(getApplicationContext(), "swim", 1).show();
}
}
});
runBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(getApplicationContext(), "run", 1).show();
}
}
});
readBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(getApplicationContext(), "read", 1).show();
}
}
});
}
}
關於mars視頻上,mars提出的對CompoundButton.OnCheckedChangeListener和RadioGroup.OnCheckedChangeListener兩個接口爲什麼不合並的問題
我提一些自己的觀點
我認爲這兩個接口本來就不應該合併,原因如下
根據文檔,我們可以看出CompoundButton是CheckBox和RadioBox的父類,而RadioGroup的直接父類居然是LinearLayout
RadioGroup從父類繼承的字段有HORIZENAL,VERTICAL
從父類繼承的方法有setGravity(),setOrientation()等等
於是可以認爲RadioGroup就是一個嵌套在外層Layout中的Layout
而RadioGroup之所以能實現衆多radioButton的單選問題,是因爲它維護了一個mCheckedId字段 記錄了現在選中的RadioButton的id(見源碼)。可以料想,安卓內部是根據這個id來做radio是否選中的顯示的。
正因爲如此,RadioGroup.OnCheckedChangeListener中onCheckedChanged方法纔是這樣的:(參數是RadioGroup和checkedId)
public void onCheckedChanged(RadioGroup group, int checkedId);
這樣我們就可以通過group這個參數來操作同組其他radio的的狀態(比如可以group.findViewById(...) 然後調用setEnabled(false)來灰化這個按鈕.或者設置所有的radio無效,等等)
總之,我想說,radiogroup中的所有的RadioButton是在組內相關的,RadioGroup維護了組內radiobutton的相關性
CompoundButton.OnCheckedChangeListener中onCheckedChanged方法是這樣的:
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked);(參數是buttonView(也就是checkbox)和checkedId)
兩個接口雖然同名,接口定義的方法雖然也同名,但是參數不一樣,所以指示的功能不一樣,所以他們是不一樣的方法,所以可以認爲是不一樣的接口。
如果兩個接口合併了,也就是RadioGroup中不定義這個Listener,而直接用RadioButton父類CompoundButton的Listener。那麼RadioButton就不能得到其他radio的狀態,得不到這個組的狀態,和checkbox又有什麼區別呢?
或許有的人要說,在checkbox的listener中也能得到其他checkbox的狀態,只要通過父類View的findViewById()就行了,這樣看來radiobutton如果用checkbox的監聽也是可以實現單選功能的。但是在我看來,這種方案使得代碼糅雜,radio的邏輯是N只能選1,這樣的底層的邏輯卻在實現層去做,這樣的設計誰都不能接受吧。
所以我認爲android設計RadioGroup是必要的,給RadioGroup定義一個屬於自己、滿足radio單選功能的內部監聽接口也是必要的。
綜上:CompoundButton的OnCheckedChangeListener只是碰巧和RadioGroup的OnCheckedChangeListener重名罷了,因爲是不同的功能,所以談不上合併
我相信這個問題肯定有更好的方法來解讀~以上只是一個Android初學者的思考