android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现

首先看到selector的属性:
android:state_focused
android:state_pressed
android:state_selected
android:state_enabled
它们之间的执行是 有执行顺序的写xml的时候特别要分析好执行顺序,否则达不到要实现的效果:

现在实现效果如下:
android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现


当点击停止按钮时,

android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现

有点击效果,和选中效果。
具体代码如下:
暂停:


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:andro>


    <item android:drawable="@drawable/icon_pause_active" android:state_pressed="true"/>
    <item android:drawable="@drawable/icon_pause_active" android:state_selected="true"/>
    <item android:drawable="@drawable/icon_pause_inactive" android:state_enabled="true"/>


</selector>

停止;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:andro>


<item android:state_pressed="true" android:drawable="@drawable/icon_stop_active"/>
<item android:state_selected="true" android:drawable="@drawable/icon_stop_active"/>
<item android:state_enabled="true" android:drawable="@drawable/icon_stop_inactive"/>

播放:
</selector>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:andro >
  <item android:state_pressed="true" android:drawable="@drawable/icon_play_active"/>
<item android:state_selected="true" android:drawable="@drawable/icon_play_active"/>
<item android:state_enabled="true" android:drawable="@drawable/icon_play_inactive"/>


</selector>

布局中:


 <ImageButton
                android:layout_marginRight="15dp"
                android:background="@null"
                android:
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src=" @drawable/music_stop_style" />
            <ImageButton
                android:
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:layout_marginRight="15dp"
                android:layout_marginLeft="15dp"
                android:src=" @drawable/music_play_style" />


            <ImageButton
                android:layout_marginLeft="15dp"
                android:
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:src="@ drawable/music_pause_style" />


代码中:
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.stop:
stop.setSelected(true);
play.setSelected(false);
pause.setSelected(false);
break;
case R.id.play:
stop.setSelected(false);
play.setSelected(true);
pause.setSelected(false);
break;
case R.id.pause:
stop.setSelected(false);
play.setSelected(false);
pause.setSelected(true);
break;

     处理好即可、。

 

相关文章:

  • 2022-12-23
  • 2022-03-07
  • 2022-12-23
  • 2021-12-17
  • 2021-07-13
  • 2021-06-20
猜你喜欢
  • 2021-10-05
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-12
相关资源
相似解决方案