前言
最近一直在講androidUI控件的使用方式,這篇博客講解一下基本上屬於用處最廣泛的控件之一的Button控件。如果有過其他平台開發經驗的程序員,對按鈕是不會陌生的。本篇博客首先講解一下Android的Button控件的常用事件以及事件綁定和觸發,再在Button控件中通過設定屬性值來實現圖文混排,這個功能是在項目中常用到的。
Button控件
Button繼承了TextView。它的功能就是提供一個按鈕,這個按鈕可以供用戶點擊,當用戶對按鈕進行操作的時候,觸發相應事件,如點擊,觸摸。
還有一個ImageButton,它繼承自Button,可以在ImageButton中顯示一個圖片展示給用戶看,並且對其Text屬性設置值的時候是無效的,其它功能與Button一樣。
常用事件
一般對於一個按鈕而言,用的最多的就是點擊事件,Button間接繼承自View,而AndroidUI中的所有事件,都是定義在View中的。在本篇博客中,示例講解的點擊事件、觸摸事件,其他事件的使用方式與此類似,只是觸發的時機不同而已。此處分別需要實現View.OnClickListener、View.OnTouchListener接口的方法。
- View.OnClickListener,需要實現onClick(View v)方法,其中v為當前觸發事件的控件。
- View.OnTouchListener,需要實現onTouch(View v , MotionEvent event),其中v為當前觸發事件的控件,event包括了觸摸時的具體內容,如移動、按下等。
下面使用一個示例講解一下事件的綁定及觸發,在示例中顯示兩個按鈕控件,一個為普通按鈕,一個為填充圖片的按鈕,為它們綁定click事件,當點擊事件觸發的時候,對其尺寸進行修改,為圖片按鈕綁定觸摸事件,當觸摸的時候觸發,切換圖片顯示。
布局代碼:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://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 } else { 41 btn.setBackgroundResource(R.drawable.image2); 42 } 43 return false; 44 } 45 46 @Override 47 public void onClick(View v) { 48 Button btn = (Button) v; 49 if (flag == 1 50 && btn.getWidth() == getWindowManager().getDefaultDisplay() 51 .getWidth()) { 52 // 如果等於屏幕的寬度,則修改標識flag為-1 53 flag = -1; 54 } else if (flag == -1 && btn.getWidth() < 100) { 55 flag = 1; 56 } 57 // 設置button控件尺寸 58 btn.setWidth(btn.getWidth() + (int) (btn.getWidth() * 0.1) * flag); 59 btn.setHeight(btn.getHeight() + (int) (btn.getHeight() * 0.1) * flag); 60 } 61 }
展示效果圖:
當點擊按鈕的時候,按鈕被放大,當放大到屏幕寬度時,開始縮小。
當觸摸圖標按鈕的時候,圖像改變。
圖文混排
對於在實際項目中,經常會需要設置按鈕展示為圖文混排的效果,這樣可以通過圖表更直觀的把按鈕的功能展示給用戶,又可以有簡短的文字說明。雖然ImageButton也可以實現圖片按鈕的效果,但是對於ImageButton而言,設置Text屬性是沒有作用的,所以這里不講解ImageButton的使用。對於Button控件,圖文混排需要用到一個android:drawableXxx屬性(Xxx為圖片所在按鈕的方向),這個屬性配合android:text,就可以實現圖文混排的效果。
下面一個示例,分別展示了上下左右四個方位的圖標按鈕,並且生成一個通過Java代碼動態生成圖文混排按鈕的。因為Button是繼承自TextView的,所以通過代碼設置圖文混排的方式與TextView類似,都需要用到SpannableString類,關於SpannableString的詳細講解可以參見我的另外一篇博客,Android--UI之TextView,這里就不再詳細講解了。
布局代碼:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://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 }
效果展示:
總結
對於實際項目而言,一般按鈕的樣式都會通過額外的XML樣式文件包裝一下,這個之后再介紹,這里只是介紹一下Button的簡單使用。按鈕的最多用處就是供用戶點擊從而觸發相應時間,沒有什么難點。
請支持原創,尊重原創,轉載請注明出處。謝謝。