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

首先看到selector的属性:
android:state_focusedandroid:state_pressedandroid:state_selectedandroid:state_enabled
它们之间的执行是 有执行顺序的写xml的时候特别要分析好执行顺序,否则达不到要实现的效果:
现在实现效果如下: android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现

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

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

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


<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:android="http://schemas.android.com/apk/res/android">


<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:android="http://schemas.android.com/apk/res/android" >
<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:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src=" @drawable/music_stop_style" />
<ImageButton
android:id="@+id/play"
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:id="@+id/pause"
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;

处理好即可、。

更多相关文章

  1. android代码库之textview跑马灯效果
  2. 自定义动画(animation)时set中的各元素效果
  3. TextView跑马灯效果
  4. 把TextView中的文字添加阴影效果及Style的使用
  5. android开发,修改默认界面的背景色
  6. Android Textview 阴影效果
  7. android 5.0以上版本控件点击 水波纹效果
  8. android手机安全卫士、Kotlin漫画、支付宝动画、沉浸状态栏等源
  9. Android标题栏、状态栏、全屏

随机推荐

  1. android 音频管理:AudioPolicyService 和
  2. android之AlertDialog 点击其他区域自动
  3. Android—Preference详解之Preference系
  4. android中的category静态值
  5. GmSSL Android端 :使用Android(安卓)Studi
  6. Android读取彩信附件
  7. Android改变图片背景颜色tint(着色)或 ba
  8. Android(安卓)程式开发:(七)处理变化 —— 7
  9. Android中蓝牙的基本使用
  10. Android(安卓)SDK4.2 (API17) 开发环境的