一般情況下,我們可以用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