作者:刘昊昱

博客:http://blog.csdn.net/liuhaoyutz

本文我们来看一个通过selector动态改变ImageButton背景图片的例子,下图是该程序的运行效果:

该程序中有三个ImageButton,最上面的是鼠标按下时的状态;中间的是鼠标没有按下,但是按钮获得了焦点的状态;最下面的是鼠标没有按下,按钮也不具有焦点的状态。

先来看主布局文件main.xml,其内容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <ImageButton
android:id="@+id/button1"
android:src="@drawable/button_image_selector"
android:focusableInTouchMode="true"
android:background="#0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton> <ImageButton
android:id="@+id/button2"
android:src="@drawable/button_image_selector"
android:focusableInTouchMode="true"
android:background="#0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton> <ImageButton
android:id="@+id/button3"
android:src="@drawable/button_image_selector"
android:focusableInTouchMode="true"
android:background="#0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton> </LinearLayout>

在线性布局控件中,放置三个ImageButton,android:src属性指定了背景图片选择器button_image_selector.xml,android:focusableInTouchMode属性标明按钮可以获得焦点。android:background="#0000"属性是为了去掉按钮背后的灰色边框,但是这样做,如果不用背景选择器的话,会看不出点击效果。

按钮背景图片选择器button_image_selector.xml定义如下:

<?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/image01"/>
<item android:state_pressed="false" android:state_focused="true" android:drawable="@drawable/image02"/>
<item android:state_pressed="false" android:state_focused="false" android:drawable="@drawable/image03"/>
</selector>

item1指定了鼠标按下时的背景图片,item2指定了鼠标没有按下,但是获得焦点时的背景图片,item3指定了鼠标没有按下,且没有焦点时的背景图片。

主Activity文件没有什么特别的,其内容如下:

package com.liuhaoyu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

Android应用开发学习之Selector的更多相关文章

  1. Android应用开发学习之表格视图

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 本文我们来学习一个使用表格视图的程序,下图是该程序的运行效果: 该程序主Activity文件内容如下: packag ...

  2. 2021年正确的Android逆向开发学习之路

    2021年正确的Android逆向开发学习之路 说明 文章首发于HURUWO的博客小站,本平台做同步备份发布.如有浏览或访问异常或者相关疑问可前往原博客下评论浏览. 原文链接 2021年正确的Andr ...

  3. Android应用开发学习笔记之播放音频

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android支持常用音视频格式文件的播放,本文我们来学习怎样开发Android应用程序对音视频进行操作. Andr ...

  4. android移动开发学习笔记(二)神奇的Web API

    本次分两个大方向去讲解Web Api,1.如何实现Web Api?2.如何Android端如何调用Web Api?对于Web Api是什么?有什么优缺点?为什么用WebApi而不用Webservice ...

  5. Android应用开发学习笔记之事件处理

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android提供的事件处理机制分为两类:一是基于监听的事件处理:二是基于回调的事件处理.对于基于监听的事件处理,主 ...

  6. Android 系统开发学习杂记(转)

    http://blog.csdn.net/shagoo/article/details/6709430 > 开发环境1.安装 Eclipse 和 android-sdk 并解压安装2.Eclip ...

  7. Android应用开发学习之相对布局

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 相对布局RelativeLayout是指按照组件之间的相对位置进行布局,如一个组件在另一个组件的左边.右边.上边或下 ...

  8. Android应用开发学习笔记之AsyncTask

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 在上一篇文章中我们学习了多线程和Handler消息处理机制,如果有计算量比较大的任务,可以创建一个新线程执行计算工作 ...

  9. Android应用开发学习笔记之Fragment

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Fragment翻译成中文就是“碎片”.“片断”的意思,Fragment通常用来作为一个Activity用户界面的一 ...

随机推荐

  1. php图片上传代码

    使用copy函数 if (!empty($_FILES)) { //图片 if(isset($_FILES['image'])) { $img_data = $_FILES['image']['tmp ...

  2. corosync+pacemaker and drbd实现mysql高可用集群

    DRBD:Distributed Replicated Block Device 分布式复制块设备,原理图如下 DRBD 有主双架构和双主架构的,当处于主从架构时,这个设备一定只有一个节点是可以读写的 ...

  3. [GUIDE] How to install Scipy in Maya Windows 64 bit - Google 网上论坛 - Google Chrome

    I've seen a lot of queries about getting scipy working in Maya (Windows 64 bit) with a few not 100% ...

  4. C#中判断bool 类型 代码的最短写法

    看到一个关于写最短代码的,  是一个bool类型判断的:    public bool IsNull(object val) { if (val == null) { return true; } e ...

  5. CAGradientLayer的一些属性解析-b

    CAGradientLayer的一些属性解析 iOS中Layer的坐标系统: 效果: - (void)viewDidLoad { [super viewDidLoad]; CAGradientLaye ...

  6. ie8下jquery读取当前点击的标签位置错误,原因是里面有内容写了text-indent:-9999px

    今天写一地图的效果,鼠标点击对应的区域,弹出所点击区域的名字. 因为设计的区域名字有特殊效果,所以,在点击区域里面套了个标签写上区域名字用来识别,但是这个文字呢不同显示在页面上,所以就给 em 加个了 ...

  7. leetcode 第五题 Longest Palindromic Substring (java)

    Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...

  8. js template

    http://garann.github.io/template-chooser/ http://www.gbin1.com/technology/javascript/20120917-javasc ...

  9. Spring+SpringMVC+Mybatis 利用AOP自定义注解实现可配置日志快照记录

    http://my.oschina.net/ydsakyclguozi/blog/413822

  10. Android 模拟登陆 保存密码(信息)到手机中 文件信息读取

    package com.wuyou.login; import java.io.IOException; import java.util.Map; import android.app.Activi ...