Android ImageButton Selector 案例

一般情况下,我们可以 用ImageButton 来显示一个Button按钮。然而,有些时候我们想按钮的状态发生变化,比如按下前是一个样子,按下后又是另一个样子,Android允许我们改变按钮的形象取决于不同的状态,如按钮是集中或按钮被按下。下面具体讲述如何实现:
P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

1.、往 Resources里面添加图片
准备三张不同Button状态的图片,然后把它放入 “resource/drawable”
1.button_normal_green.png – 默认的图像Button
2.button_focused_orange.png – 当按钮被关注,例如,当电话键盘移动(焦点)在这个按钮时显示
3.button_pressed_yellow.png – 当按钮被按下时显示

2、为不同的Button状态添加 Selector
在“res/drawable/”里面创建一个新的XML布局文件,这里我们取名为“new_button.xml“。这个XML文件定义按钮的状态是属于哪种Button图像。
File : res/drawable/new_button.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  3.     <item android:drawable="@drawable/button_pressed_yellow" android:state_pressed="true"/> 
  4.     <item android:drawable="@drawable/button_focused_orange" android:state_focused="true"/> 
  5.     <item android:drawable="@drawable/button_normal_green"/> 
  6. </selector>
复制代码
3、添加Button
打开 “res/layout/main.xml”布局文件,添加一个正常的button,然后为这个button添加一个背景图像
File : res/layout/main.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical" > 
  6.     <Button
  7.         android:id="@+id/imageButtonSelector"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:background="@drawable/new_button" /> 
  11. //通过这个方式来实现 
  12. </LinearLayout>
复制代码
4、Activity代码如下
一个正常的按钮和一个简单的点击侦听器

  1. package com.demo.app; 
  2.   
  3. import android.app.Activity; 
  4. import android.os.Bundle; 
  5. import android.view.View; 
  6. import android.view.View.OnClickListener; 
  7. import android.widget.Button; 
  8. import android.widget.Toast; 
  9.   
  10. public class AppTestActivity extends Activity { 
  11.   
  12.     Button imageButton; 
  13.   
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) { 
  16.         super.onCreate(savedInstanceState); 
  17.         setContentView(R.layout.main); 
  18.         imageButton = (Button) findViewById(R.id.imageButtonSelector); 
  19.         imageButton.setOnClickListener(new OnClickListener() { 
  20.   
  21.             public void onClick(View v) { 
  22.                 Toast.makeText(AppTestActivity.this, "eee", Toast.LENGTH_SHORT); 
  23.   
  24.             } 
  25.         }); 
  26.     } 
  27. }
复制代码
5、运行
运行该应用程序
1. 结果如图,默认的button. (button_normal_green.png)


2. Button被按下. (button_clicked_orange.png)


3. Button聚焦. (button_focused_orange.png)


取自:http://www.android-study.com/jichuzhishi/537.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值