xml文件:
<RadioGroup android:id="@+id/MusicList_RadioGroup" android:orientation="horizontal" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="2.0dip" android:background="@drawable/radiogroup_bg" > <RadioButton android:id="@+id/MusicList_RadioGroup_next" android:drawableLeft="@drawable/radiogroup_next" android:text="@string/Text_radiogroup_next" android:textSize="15.0dip" android:layout_weight="1.0" android:button="@null" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/MusicList_RadioGroup_playAndpuse" android:drawableLeft="@drawable/radiogroup_play" android:text="@string/Text_radiogroup_play" android:textSize="15.0dip" android:layout_weight="1.0" android:button="@null" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/MusicList_RadioGroup_previous" android:drawableLeft="@drawable/radiogroup_previous" android:text="@string/Text_radiogroup_previous" android:textSize="15.0dip" android:layout_weight="1.0" android:button="@null" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RadioGroup>
//綁定監聽器 MusicListTable_RadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){ public void onCheckedChanged(RadioGroup arg0, int rid) { switch(rid) { case R.id.MusicList_RadioGroup_next://下一首 break; case R.id.MusicList_RadioGroup_previous://上一首 break; case R.id.MusicList_RadioGroup_playAndpuse://播放或暫停 if(isPlaying) { Drawable dr= res.getDrawable(R.drawable.radiogroup_play); //setBounds如果不設置的話setCompoundDrawables就會沒有圖片顯示出來,所以一定要設置一次 dr.setBounds(0, 0, dr.getMinimumWidth(), dr.getMinimumHeight()); palyAndpuse.setCompoundDrawables(dr, null,null, null);//爲RadioButton設置圖片,左右上下對應xml的android:drawableLeft="@drawable/XXX" isPlaying=false; } else { Drawable dr= res.getDrawable(R.drawable.radiogroup_puse); dr.setBounds(0, 0, dr.getMinimumWidth(), dr.getMinimumHeight()); palyAndpuse.setCompoundDrawables(dr, null,null, null); isPlaying=true; } arg0.clearCheck();//清除選擇,如果不清除的話不能重複選擇同一個Radiobutton break; } }}); }
效果: