9、從頭學Android之Button和ImageButton

來源:互聯網
上載者:User

 

Button

類結構圖:

由類結構圖,可以看出Button是繼承於TextView的,所以TextView的一些屬性也適用於Button控制項。對於Button控制項,相信大多數開發人員是再熟悉不過了,那麼廢話不多說,直接應用實踐吧

 

實踐演練:

1、  如何設定按鈕的樣式?

通過Android:background設定 

 <Button android:id="@+id/myBtn1" android:text="按鈕1 設定背景樣式"      android:layout_width="fill_parent" android:layout_height="wrap_content"      android:background="#fff000" />

2、如何設定背景表徵圖: 

<Button android:id="@+id/myBtn6" android:text="按鈕6 設定背景表徵圖"      android:layout_width="wrap_content" android:layout_height="wrap_content"      android:textStyle="bold" android:background="@drawable/back_48"       />

3、  如何設定按鈕的文字顏色

通過Android:textColor

 <Button android:id="@+id/myBtn2" android:text="按鈕2 字型顏色"      android:layout_width="fill_parent" android:layout_height="wrap_content"      android:textColor="#ff0000" /> 

4、  如何設定按鈕的文字樣式

通過android:textStyle  

 <Button android:id="@+id/myBtn3" android:text="按鈕3字型加粗"      android:layout_width="fill_parent" android:layout_height="wrap_content"      android:textColor="#ff0000" android:textStyle="bold" />

5、  如何為按鈕添加監聽器註冊事件

 方式1:通過setOnClickListener方式

myBtn4.setOnClickListener(new OnClickListener() {                      @Override                     public void onClick(View v) {                            myBtn4.setText("setOnclickListener事件監聽註冊成功");                      }              });

方式2:通過XML檔案的Android:onClick指定方法 

<Button android:id="@+id/myBtn4" android:text="按鈕4 通過setOnclickListener註冊監聽事件"      android:layout_width="fill_parent" android:layout_height="wrap_content"      android:textStyle="bold" android:onClick="selfDestruct" />

指定了sefDestrut

所以在Activity寫上一個這樣的方法就可以了

public void selfDestruct(View v) {       myBtn5.setText("XML方式事件監聽註冊成功");      System.out.println("------view v--------");    }

 

上面的綜合例子為:

 

 

 

ImageButton

類結構圖:

ImageButton就是用一個表徵圖代表了一些文字,它沒Android:text屬性。它由Android:src指定表徵圖的位置

android:src="@drawable/back_48"

其他屬性都和Button差不多

實踐演練:

1、  怎麼樣設定ImageButton的表徵圖位置

   <ImageButton android:id="@+id/imgBtn01" android:src="@drawable/forward_48"      android:layout_width="wrap_content" android:layout_height="wrap_content" />

2、  怎麼樣為ImageButton添加監聽器註冊事件

與Button一樣它照樣有兩種方式設定

方式1:通過onClickListener

imgBtn01.setOnClickListener(new OnClickListener() {         @Override        public void onClick(View v) {            myTextView.setText("ImageButton的監聽事件");        }      });

方式2:通過XML檔案設定

<ImageButton android:id="@+id/imgBtn02" android:src="@drawable/back_48"      android:layout_width="wrap_content" android:layout_height="wrap_content"      android:onClick="ImageButtonXml" />

最終效果:

 

1、  設定透明度

imgBtn01.setAlpha(50);//設定透明度

 

聯繫我們

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