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; } }}); }
效果:

点击播放不断切换两个图片

转自:http://www.cnblogs.com/helloandroid/archive/2011/08/23/2151164.html
本文介绍了一个使用XML布局的音乐播放器控制按钮实现方法,通过RadioGroup包含三个RadioButton来表示上一曲、播放/暂停和下一曲功能,并展示了如何通过监听器实现按钮状态的切换。

4万+

被折叠的 条评论
为什么被折叠?



