展会信息港展会大全

Android ImageButton Selector例子
来源:互联网   发布日期:2015-12-07 15:34:11   浏览:1833次  

导读: 一般情况下,我们可以用ImageButton来显示一个Button按钮。然而,有些时候我们想按钮的状态发生变化,比如按下前是一个样子,按下后又是另一个样子,Android允许我们改变按钮的...

一般情况下,我们可以用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” 。

button_normal_green.png – 默认的图像Button.

button_focused_orange.png – 当按钮被关注,例如,当电话键盘移动(焦点)在这个按钮时显示。

button_pressed_yellow.png – 当按钮被按下时显示

2. 为不同的Button状态添加 Selector

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

File : res/drawable/new_button.xml

[xml]

<SPAN style="COLOR: rgb(0,153,0)"><SPAN style="COLOR: rgb(0,0,0)"><STRONG></STRONG></SPAN></SPAN><?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/button_pressed_yellow"

android:state_pressed="true" />

<item android:drawable="@drawable/button_focused_orange"

android:state_focused="true" />

<item android:drawable="@drawable/button_normal_green" />

</selector><SPAN style="COLOR: rgb(0,153,0)"><SPAN style="COLOR: rgb(0,0,0)"><STRONG></STRONG></SPAN></SPAN>

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/button_pressed_yellow"

android:state_pressed="true" />

<item android:drawable="@drawable/button_focused_orange"

android:state_focused="true" />

<item android:drawable="@drawable/button_normal_green" />

</selector>3.添加Button

打开 “res/layout/main.xml”布局文件,添加一个正常的button,然后为这个button添加一个背景图像

File : res/layout/main.xml

[xml]

<SPAN style="COLOR: rgb(0,153,0)"><SPAN style="COLOR: rgb(0,0,0)"><STRONG></STRONG></SPAN></SPAN><?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" >

<Button

android:id="@+id/imageButtonSelector"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/new_button" />//通过这个方式来实现

</LinearLayout><SPAN style="COLOR: rgb(0,153,0)"><SPAN style="COLOR: rgb(0,0,0)"><STRONG></STRONG></SPAN></SPAN>

<?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" >

<Button

android:id="@+id/imageButtonSelector"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/new_button" />//通过这个方式来实现

</LinearLayout>4.Activity代码如下

一个正常的按钮和一个简单的点击侦听器

package com.demo.app;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class AppTestActivity extends Activity{

/* (non-Javadoc)

* @see android.app.Activity#onCreate(android.os.Bundle)

*/

ButtonimageButton;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);www.2cto.com

setContentView(R.layout.main);

imageButton=(Button) findViewById(R.id.imageButtonSelector);

imageButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Toast.makeText(AppTestActivity.this, "eee", Toast.LENGTH_SHORT);

}

});

}

}

5.运行

运行该应用程序

1. 结果如图, 默认的 button. (button_normal_green.png)

2. Button 聚焦. (button_focused_orange.png)

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

这个应用其实还是很广泛的,比如播放器的控制,游戏的状态等等。

作者:wangjinyu501

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港