Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 應用開發入門(十九):Button 控件事件綁定與圖文混排實踐

編輯:Android 開發實例

導讀

Button 大概是用得最泛的控件之一,有過其他平台經驗的開發者對它不會感到陌生。本文分兩塊:先講事件體系——點擊與觸摸的綁定和觸發;再講實戰中極常見的圖文混排按鈕,包括 XML 屬性寫法與 Java 動態拼接寫法。

Button 與 ImageButton

Button 繼承自 TextView,職責很單一:提供一個可點擊的區域,在用戶操作時觸發相應事件。

另有 ImageButton 繼承自 Button,可以把圖片直接鋪在按鈕上。要注意的是,對 ImageButton 設置 text 屬性是不生效的——這也是下文選擇用 Button 而非 ImageButton 做圖文混排的原因。

常用事件

Button 間接繼承自 View,而 Android UI 的事件全部定義在 View 之中。本文演示點擊與觸摸兩種,其餘事件的使用方式同理,只是觸發時機不同。

  • View.OnClickListener:實現 onClick(View v),v 為觸發事件的控件。
  • View.OnTouchListener:實現 onTouch(View v, MotionEvent event),event 攜帶了按下、移動、抬起等具體動作信息。

兩者的分工值得記住:onTouch 回報的是完整的指頭動作序列,粒度更細、觸發更頻;onClick 則是系統已經幫你判斷出「這算一次有效點擊」之後才回調。做按下態視覺反饋用 onTouch,做業務提交用 onClick。

下面示例放兩個按鈕:普通按鈕綁定 click,反覆點擊會逐步放大、到達屏幕寬度後轉為縮小;圖片按鈕綁定 touch,按下與抬起時切換背景圖。

布局代碼:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:orientation="vertical" > 
  6.     
  7.     <Button 
  8.         android:id="@+id/btnChangeSize" 
  9.         android:layout_width="wrap_content" 
  10.         android:layout_height="wrap_content" 
  11.         android:text="點擊修改尺寸" 
  12.         /> 
  13.     <Button 
  14.         android:id="@+id/btnChangeImg" 
  15.         android:layout_width="wrap_content" 
  16.         android:layout_height="wrap_content" 
  17.         android:background="@drawable/image1" 
  18.         /> 
  19. </LinearLayout> 

實現代碼:

  1. package com.bgxt.buttondemo;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.MotionEvent;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.View.OnTouchListener;  
  9. import android.widget.Button;  
  10.  
  11. //通過實現接口,對其進行click、touch事件的支持 
  12. public class ButtonListener extends Activity implements OnClickListener, 
  13.         OnTouchListener {  
  14.  
  15.     private Button btnChangeSize;  
  16.     private Button btnChangeImg;  
  17.     private int flag = 1;  
  18.  
  19.     @Override 
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.btn_listener);  
  23.  
  24.         btnChangeSize = (Button) findViewById(R.id.btnChangeSize);  
  25.         btnChangeImg = (Button) findViewById(R.id.btnChangeImg);  
  26.  
  27.         // 對兩個按鈕進行事件綁定 
  28.         btnChangeSize.setOnClickListener(this);  
  29.         btnChangeImg.setOnClickListener(this);  
  30.         btnChangeImg.setOnTouchListener(this);  
  31.     }  
  32.  
  33.     @Override 
  34.     public boolean onTouch(View v, MotionEvent event) {  
  35.         // 獲取觸發事件的Button控件 
  36.         Button btn = (Button) v;  
  37.         if (event.getAction() == MotionEvent.ACTION_UP) {  
  38.             // 當觸摸抬起時,還原展示圖片為image1 
  39.             btn.setBackgroundResource(R.drawable.image1);  
  40.         }  
  41.         else {  
  42.             btn.setBackgroundResource(R.drawable.image2);  
  43.         }  
  44.         return false;  
  45.     }  
  46.  
  47.     @Override 
  48.     public void onClick(View v) {  
  49.         Button btn = (Button) v;  
  50.         if (flag == 1 
  51.                 && btn.getWidth() == getWindowManager().getDefaultDisplay() 
  52.                      .getWidth()) {  
  53.                 // 如果等於屏幕的寬度,則修改標識flag為-1 
  54.                 flag = -1;  
  55.         }  
  56.         else if (flag == -1 && btn.getWidth() < 100) {  
  57.                 flag = 1;  
  58.         }  
  59.         // 設置button控件尺寸 
  60.         btn.setWidth(btn.getWidth() + (int) (btn.getWidth() * 0.1) * flag);  
  61.         btn.setHeight(btn.getHeight() + (int) (btn.getHeight() * 0.1) * flag);  
  62.     }  
  63. } 

兩個細節:一是 onTouch 返回 false,把事件繼續交給後續處理,若返回 true 則會吞掉事件,onClick 也不會再觸發;二是用一個 flag 標識位控制放大/縮小的方向,避免尺寸越界。

圖文混排

實戰中經常需要「圖標 + 簡短文字」的按鈕形式,既直觀又能補充說明。Button 繼承自 TextView,因此具備 android:drawableXxx 系列屬性(Xxx 代表圖標方位),配合 android:text 就能拼出圖文混排:

  • android:drawableTop / Bottom / Left / Right:分別把圖標放在文字的上、下、左、右。
  • android:drawablePadding:圖標與文字之間的間距。

若想在代碼中動態完成,思路與 TextView 一致:借助 SpannableString 包裹 ImageSpan,再 append 進按鈕(SpannableString 的詳細機制可參考作者專文)。

下面示例一次性展示四個方位的 XML 寫法,並額外生成一個純代碼拼接的按鈕。

布局代碼:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:orientation="vertical" > 
  6.  
  7.     <LinearLayout 
  8.         android:layout_width="match_parent" 
  9.         android:layout_height="150dp" 
  10.         android:orientation="horizontal" > 
  11.         <!-- 圖片在上,項目中常用這樣的設置 --> 
  12.         <Button 
  13.             android:layout_width="wrap_content" 
  14.             android:layout_height="wrap_content" 
  15.             android:drawableTop="@drawable/image2" 
  16.             android:text="b1" /> 
  17.         <!-- 圖片在下 --> 
  18.         <Button 
  19.             android:layout_width="wrap_content" 
  20.             android:layout_height="wrap_content" 
  21.             android:drawableBottom="@drawable/image2" 
  22.             android:drawablePadding="10dp" 
  23.             android:text="b2" /> 
  24.         <!-- 圖片在左 --> 
  25.         <Button 
  26.             android:layout_width="wrap_content" 
  27.             android:layout_height="wrap_content" 
  28.             android:drawableLeft="@drawable/image2" 
  29.             android:text="b3" /> 
  30.         <!-- 圖片在右 --> 
  31.         <Button 
  32.             android:layout_width="wrap_content" 
  33.             android:layout_height="wrap_content" 
  34.             android:drawablePadding="10dp" 
  35.             android:drawableRight="@drawable/image2" 
  36.             android:text="b4" /> 
  37.     </LinearLayout> 
  38.     <!-- 聲明一個空的按鈕,用於進行代碼設置 --> 
  39.     <Button android:layout_width="200dp" android:layout_height="200dp" android:id="@+id/btnSty" android:layout_marginTop="10dp"/> 
  40. </LinearLayout> 

Java 實現代碼:

  1. package com.bgxt.buttondemo;  
  2.  
  3. import android.app.Activity;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.os.Bundle;  
  7. import android.text.SpannableString;  
  8. import android.text.Spanned;  
  9. import android.text.style.ImageSpan;  
  10. import android.widget.Button;  
  11.  
  12. public class ButtonStyle extends Activity {  
  13.  
  14.     private Button btnSty;  
  15.     @Override 
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         // TODO Auto-generated method stub 
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.btn_style);  
  20.         //獲取按鈕控件 
  21.         btnSty=(Button)findViewById(R.id.btnSty);  
  22.         //生成SpannableString,用於圖片的載體 
  23.         SpannableString spannebleLeft=new SpannableString("left");  
  24.         Bitmap bitmapleft=BitmapFactory.decodeResource(getResources(), R.drawable.image1);  
  25.         ImageSpan imageSpanLeft=new ImageSpan(ButtonStyle.this,bitmapleft);  
  26.         spannebleLeft.setSpan(imageSpanLeft,0,4,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  27.         
  28.         SpannableString spannebleRight=new SpannableString("right");  
  29.         Bitmap bitmapRight=BitmapFactory.decodeResource(getResources(), R.drawable.image2);  
  30.         ImageSpan imageSpanRight=new ImageSpan(ButtonStyle.this,bitmapRight);  
  31.         spannebleRight.setSpan(imageSpanRight,0,5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  32.         
  33.         //把生成的SpannableString追加到按鈕上 
  34.         btnSty.append(spannebleLeft);  
  35.         btnSty.append("aLi");  
  36.         btnSty.append(spannebleRight);  
  37.     }  
  38. } 

setSpan 的第三、四個參數是索引區間,必須與占位文本的長度對應,否則圖標會蓋錯位置或完全不顯示。

小結

Button 的難點不在控件本身,而在事件模型的選擇與圖文組合方式。實際項目中按鈕外觀通常還會再包一層 XML 樣式或 selector(例如按下態變色),這部分留待後續文章;本文先把基礎打牢。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved