雜亂之android的ImageButton應用

來源:互聯網
上載者:User

先:(圖中為兩個按鈕,可不是兩個圖片,嘿嘿)

第一個按鈕為ImageButton,第二個按鈕為Button。

imagebutton.xml代碼

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout<br /> xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:orientation="vertical"<br /> ><br /> <!-- 可以在這裡通過android:src="@drawable/icon"設定顯示在按鈕上的圖片 --><br /> <ImageButton<br /> android:id="@+id/button_imageButton"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> /><br /> <Button<br /> android:id="@+id/button_Button"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:background="@drawable/sbtn5_2"<br /> android:text="goggle一下"<br /> ></Button><br /></LinearLayout><br />

 

activity的代碼如下所示:

package cn.com.chenzheng_java;</p><p>import android.app.Activity;<br />import android.content.res.Resources;<br />import android.graphics.drawable.Drawable;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.widget.ImageButton;<br />import android.widget.Toast;<br />/**<br /> * @description ImageButton的應用測試<br /> * @author chenzheng_java<br /> * @since 2011/03/09<br /> */<br />public class ImageButtonActivity extends Activity {</p><p>@Override<br />protected void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.imagebutton);<br />ImageButton imageButton = (ImageButton)findViewById(R.id.button_imageButton);</p><p>// imageButton.setBackgroundResource(R.drawable.excel);設定表徵圖方法一</p><p>//設定表徵圖方法二<br />Resources resources = this.getResources();<br />Drawable d = resources.getDrawable(R.drawable.excel);<br />imageButton.setBackgroundDrawable(d);</p><p>imageButton.setOnClickListener(new View.OnClickListener() {</p><p>@Override<br />public void onClick(View v) {<br />Toast.makeText(ImageButtonActivity.this, "clicked", Toast.LENGTH_LONG).show();</p><p>}<br />});</p><p>}<br />}<br />

其他的均為預設。運行便可以得到相應的結果。 

-----------------------------------------------------------------

ImageButton繼承自Button,其主要的特點就是上面顯示的內容不再是文字,而是一張圖片。

在布局檔案中,我們可以通過android:src屬性為其設定圖片。而在代碼中,我們則可以通過setBackgroundDrawable()/setBackgroundResource()/setBackgroundColor進行設定。

這裡要注意,ImageButton是不支援在圖片上面再放置一些文字的,如果想要實現這種功能,就應該跟我們的第二個按鈕一樣,用Button按鈕來實現,通過設定android:text和android:background兩個屬性配合來實現。

-------------------------------------------------------------------------

Button進階:

你是不是經常看到很多商業應用中,對於按鈕,當擷取焦點和失去焦點時顯示的效果不一樣呢?我們當然可以想象到,想要實現該種效果,我們只需要捕捉相應的事件然後再在代碼中動態實現。但是,你不覺得這樣很麻煩嗎?那到底有沒有一種更加方便的方式讓我們實現這種應用呢?

      有!

現在我們就來介紹下,這種方式:

 

在res/drawable/檔案夾下建立一個xml檔案,imagebutton2.xml,代碼如下

<?xml version="1.0" encoding="utf-8" ?><br /><selector xmlns:android="http://schemas.android.com/apk/res/android"><br /><item android:state_window_focused="false" android:drawable="@drawable/excel" /><br /><item android:state_focused="true" android:state_pressed="true"<br />android:drawable="@drawable/icon" /><br /><item android:state_focused="false" android:state_pressed="true"<br />android:drawable="@drawable/baidu" /><br /><item android:state_selected="true" android:drawable="@drawable/sbtn5_2" /><br /><item android:state_focused="true" android:drawable="@drawable/icon" /><br /></selector><br /><!--<br /><selector xmlns:android="http://schemas.android.com/apk/res/android"><br /><item android:state_window_focused="false" android:drawable="@drawable/沒有焦點時的圖片背景" /><br /><item android:state_focused="true" android:state_pressed="true"<br />android:drawable="@drawable/非觸摸模式下獲得焦點並單擊時的背景圖片" /><br /><item android:state_focused="false" android:state_pressed="true"<br />android:drawable="@drawable/觸摸模式下單擊時的背景圖片" /><br /><item android:state_selected="true" android:drawable="@drawable/選中時的圖片背景" /><br /><item android:state_focused="true" android:drawable="@drawable/獲得焦點時的圖片背景" /><br /></selector><br /> --><br />

然後再聲明Button按鈕的地方為其設定android:background="@drawable/imagebutton2"屬性,然後我們就可以看到想要的結果了。

(這裡的測試結果是,除了按下之外,其他的設定都不管用,代碼是沒有問題的,問題應該出在模擬器上,我沒有android手機,所以不能實踐了,讀者自己試試吧)

這裡粘貼網上的一個資源說明。關於selector的用法,我會在最近儘快給大家搞定的。

忽然間在ImageButton的api裡看到了這麼一段關於selector的介紹,這裡粘貼出來給大家看看。從介紹我們可以看出,我們的用法是沒有錯誤的,只是什麼操作會觸發什麼事件並不明確罷了。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.