Android ImageButton Selector例子

來源:互聯網
上載者:User

  一般情況下,我們可以用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)
*/
Button  imageButton;
@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
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.