歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android自定義控件之我的電話小鍵盤

Android自定義控件之我的電話小鍵盤

日期:2017/3/1 11:09:42   编辑:Linux編程

關於Android的自定義控件,之前也寫了兩個,一個是簡單地繼承View,另一個通過繼承Layout實現一個省市聯動控件。這篇,將通過繼承ViewGroup來實現一個電話撥打小鍵盤。本人一貫風格,懶得羅裡吧嗦講一大堆,直接上圖上代碼,一切盡在注釋中!

1、MyPhoneCard.java

  1. /**
  2. *
  3. * 自定義一個4*3的撥打電話的布局控件,
  4. *
  5. *
  6. */
  7. public class MyPhoneCard extends ViewGroup{
  8. private static final int COLUMNS = 3;
  9. private static final int ROWS = 4;
  10. private static final int NUM_BUTTON = COLUMNS*ROWS;
  11. private View[] mButtons = new View[NUM_BUTTON];
  12. private int mButtonWidth;
  13. private int mButtonHeight;
  14. private int mPaddingLeft;
  15. private int mPaddingRight;
  16. private int mPaddingTop;
  17. private int mPaddingBottom;
  18. private int mWidthInc;
  19. private int mHeightInc;
  20. private int mWidth;
  21. private int mHeight;
  22. public MyPhoneCard(Context context) {
  23. super(context);
  24. }
  25. public MyPhoneCard(Context context, AttributeSet attrs){
  26. super(context,attrs);
  27. }
  28. public MyPhoneCard(Context context, AttributeSet attrs, int defStyle){
  29. super(context,attrs,defStyle);
  30. }
  31. /**
  32. * 當從xml將所有的控件都調入內存後,觸發的動作
  33. * 在這裡獲取控件的大小,並計算整個ViewGroup需要的總的寬和高
  34. */
  35. @Override
  36. protected void onFinishInflate(){
  37. super.onFinishInflate();
  38. final View[] btns = mButtons;
  39. for(int i=0; i<NUM_BUTTON; i++){
  40. btns[i] = this.getChildAt(i);
  41. btns[i].measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
  42. }
  43. //緩存大小
  44. final View child = btns[0];
  45. mButtonWidth = child.getMeasuredWidth();
  46. mButtonHeight = child.getMeasuredHeight();
  47. mPaddingLeft = this.getPaddingLeft();
  48. mPaddingRight = this.getPaddingRight();
  49. mPaddingTop = this.getPaddingTop();
  50. mPaddingBottom = this.getPaddingBottom();
  51. mWidthInc = mButtonWidth + mPaddingLeft + mPaddingRight;
  52. mHeightInc = mButtonHeight + mPaddingTop + mPaddingBottom;
  53. mWidth = mWidthInc*COLUMNS;
  54. mHeight = mHeightInc*ROWS;
  55. Log.v("Finish Inflate:", "btnWidth="+mButtonWidth+",btnHeight="+mButtonHeight+",padding:"+mPaddingLeft+","+mPaddingTop+","+mPaddingRight+","+mPaddingBottom);
  56. }
  57. /**
  58. * 這個方法在onFinishInflate之後,onLayout之前調用。這個方面調用兩次
  59. */
  60. @Override
  61. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
  62. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  63. Log.v("ViewGroup SIZE:width=", mWidth+"");
  64. Log.v("ViewGroup SIZE: height=",mHeight+"");
  65. final int width = resolveSize(mWidth, widthMeasureSpec);//傳入我們希望得到的寬度,得到測量後的寬度
  66. final int height = resolveSize(mHeight,heightMeasureSpec);//傳入我們希望得到的高度,得到測量後的高度
  67. Log.v("ViewGroup Measured SIZE: width=", width+"");
  68. Log.v("ViewGroup Measured SIZE: height=", height+"");
  69. //重新計算後的結果,需要設置。下面這個方法必須調用
  70. setMeasuredDimension(width, height);
  71. }
  72. /**
  73. * 這個方法在onMeasure之後執行,這個自定義控件中含有12個子控件(每個小鍵),所以,重寫這個方法,
  74. * 調用每個鍵的layout,將他們一個一個布局好
  75. * 就是4*3的放置,很簡單,一個嵌套循環搞定
  76. */
  77. @Override
  78. protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  79. final View[] buttons = mButtons;
  80. int i = 0;
  81. Log.v("BOTTOM:", bottom+"");
  82. Log.v("TOP", top+"");
  83. int y = (bottom - top) - mHeight + mPaddingTop;//這裡其實bottom-top=mHeight,所以y=mPaddingTop
  84. Log.v("Y=", y+"");
  85. for(int row=0; row<ROWS; row++){
  86. int x = mPaddingLeft;
  87. for(int col = 0; col < COLUMNS; col++){
  88. buttons[i].layout(x, y, x+mButtonWidth, y+mButtonHeight);
  89. x = x + mWidthInc;
  90. i++;
  91. }
  92. y = y + mHeightInc;
  93. }
  94. }
  95. }
2、布局文件:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <demo.phone.card.MyPhoneCard
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:id = "@+id/dialpad"
  5. android:paddingLeft="7dp"
  6. android:paddingRight="7dp"
  7. android:paddingTop="6dp"
  8. android:paddingBottom="6dp"
  9. android:layout_gravity="center"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_marginBottom="5dp">
  13. <ImageButton android:id="@+id/one"
  14. android:src="@drawable/dial_num_1_no_vm"
  15. style="@style/dial_btn_style"
  16. />
  17. <ImageButton android:id="@+id/two"
  18. android:src="@drawable/dial_num_2"
  19. style="@style/dial_btn_style"/>
  20. <ImageButton android:id="@+id/three"
  21. android:src="@drawable/dial_num_3"
  22. style="@style/dial_btn_style"/>
  23. <ImageButton android:id="@+id/four"
  24. android:src="@drawable/dial_num_4"
  25. style="@style/dial_btn_style"/>
  26. <ImageButton android:id="@+id/five"
  27. android:src="@drawable/dial_num_5"
  28. style="@style/dial_btn_style"/>
  29. <ImageButton android:id="@+id/six"
  30. android:src="@drawable/dial_num_6"
  31. style="@style/dial_btn_style"/>
  32. <ImageButton android:id="@+id/seven"
  33. android:src="@drawable/dial_num_7"
  34. style="@style/dial_btn_style"/>
  35. <ImageButton android:id="@+id/eight"
  36. android:src="@drawable/dial_num_8"
  37. style="@style/dial_btn_style"/>
  38. <ImageButton android:id="@+id/nine"
  39. android:src="@drawable/dial_num_9"
  40. style="@style/dial_btn_style"/>
  41. <ImageButton android:id="@+id/star"
  42. android:src="@drawable/dial_num_star"
  43. style="@style/dial_btn_style"/>
  44. <ImageButton android:id="@+id/zero"
  45. android:src="@drawable/dial_num_0"
  46. style="@style/dial_btn_style"/>
  47. <ImageButton android:id="@+id/pound"
  48. android:src="@drawable/dial_num_pound"
  49. style="@style/dial_btn_style"/>
  50. </demo.phone.card.MyPhoneCard>
這樣,就實現了上圖的小鍵盤。這個例子參考Android自帶電話應用的實現。可見,在開發中,靈活運用自定義的控件,可以實現獨特而富有魅力的效果!
Copyright © Linux教程網 All Rights Reserved