Android自定義控件之電話撥打小鍵盤
關(guān)于Android的自定義控件,之前也寫了兩個,一個是簡單地繼承View,另一個通過繼承Layout實(shí)現(xiàn)一個省市聯(lián)動控件。這篇,將通過繼承ViewGroup來實(shí)現(xiàn)一個電話撥打小鍵盤。本人一貫風(fēng)格,懶得羅里吧嗦講一大堆,直接上圖上代碼,一切盡在注釋中!
1、MyPhoneCard.java
/** * * 自定義一個4*3的撥打電話的布局控件, * * */ public class MyPhoneCard extends ViewGroup{ private static final int COLUMNS = 3; private static final int ROWS = 4; private static final int NUM_BUTTON = COLUMNS*ROWS; private View[] mButtons = new View[NUM_BUTTON]; private int mButtonWidth; private int mButtonHeight; private int mPaddingLeft; private int mPaddingRight; private int mPaddingTop; private int mPaddingBottom; private int mWidthInc; private int mHeightInc; private int mWidth; private int mHeight; public MyPhoneCard(Context context) { super(context); } public MyPhoneCard(Context context, AttributeSet attrs){ super(context,attrs); } public MyPhoneCard(Context context, AttributeSet attrs, int defStyle){ super(context,attrs,defStyle); } /** * 當(dāng)從xml將所有的控件都調(diào)入內(nèi)存后,觸發(fā)的動作 * 在這里獲取控件的大小,并計算整個ViewGroup需要的總的寬和高 */ @Override protected void onFinishInflate(){ super.onFinishInflate(); final View[] btns = mButtons; for(int i=0; i<NUM_BUTTON; i++){ btns[i] = this.getChildAt(i); btns[i].measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); } //緩存大小 final View child = btns[0]; mButtonWidth = child.getMeasuredWidth(); mButtonHeight = child.getMeasuredHeight(); mPaddingLeft = this.getPaddingLeft(); mPaddingRight = this.getPaddingRight(); mPaddingTop = this.getPaddingTop(); mPaddingBottom = this.getPaddingBottom(); mWidthInc = mButtonWidth + mPaddingLeft + mPaddingRight; mHeightInc = mButtonHeight + mPaddingTop + mPaddingBottom; mWidth = mWidthInc*COLUMNS; mHeight = mHeightInc*ROWS; Log.v("Finish Inflate:", "btnWidth="+mButtonWidth+",btnHeight="+mButtonHeight+",padding:"+mPaddingLeft+","+mPaddingTop+","+mPaddingRight+","+mPaddingBottom); } /** * 這個方法在onFinishInflate之后,onLayout之前調(diào)用。這個方面調(diào)用兩次 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ super.onMeasure(widthMeasureSpec, heightMeasureSpec); Log.v("ViewGroup SIZE:width=", mWidth+""); Log.v("ViewGroup SIZE: height=",mHeight+""); final int width = resolveSize(mWidth, widthMeasureSpec);//傳入我們希望得到的寬度,得到測量后的寬度 final int height = resolveSize(mHeight,heightMeasureSpec);//傳入我們希望得到的高度,得到測量后的高度 Log.v("ViewGroup Measured SIZE: width=", width+""); Log.v("ViewGroup Measured SIZE: height=", height+""); //重新計算后的結(jié)果,需要設(shè)置。下面這個方法必須調(diào)用 setMeasuredDimension(width, height); } /** * 這個方法在onMeasure之后執(zhí)行,這個自定義控件中含有12個子控件(每個小鍵),所以,重寫這個方法, * 調(diào)用每個鍵的layout,將他們一個一個布局好 * 就是4*3的放置,很簡單,一個嵌套循環(huán)搞定 */ @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { final View[] buttons = mButtons; int i = 0; Log.v("BOTTOM:", bottom+""); Log.v("TOP", top+""); int y = (bottom - top) - mHeight + mPaddingTop;//這里其實(shí)bottom-top=mHeight,所以y=mPaddingTop Log.v("Y=", y+""); for(int row=0; row<ROWS; row++){ int x = mPaddingLeft; for(int col = 0; col < COLUMNS; col++){ buttons[i].layout(x, y, x+mButtonWidth, y+mButtonHeight); x = x + mWidthInc; i++; } y = y + mHeightInc; } } }
2、布局文件:
<?xml version="1.0" encoding="utf-8"?> <demo.phone.card.MyPhoneCard xmlns:android="http://schemas.android.com/apk/res/android" android:id = "@+id/dialpad" android:paddingLeft="7dp" android:paddingRight="7dp" android:paddingTop="6dp" android:paddingBottom="6dp" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp"> <ImageButton android:id="@+id/one" android:src="@drawable/dial_num_1_no_vm" style="@style/dial_btn_style" /> <ImageButton android:id="@+id/two" android:src="@drawable/dial_num_2" style="@style/dial_btn_style"/> <ImageButton android:id="@+id/three" android:src="@drawable/dial_num_3" style="@style/dial_btn_style"/> <ImageButton android:id="@+id/four" android:src="@drawable/dial_num_4" style="@style/dial_btn_style"/> <ImageButton android:id="@+id/five" android:src="@drawable/dial_num_5" style="@style/dial_btn_style"/> <ImageButton android:id="@+id/six" android:src="@drawable/dial_num_6" style="@style/dial_btn_style"/> <ImageButton android:id="@+id/seven" android:src="@drawable/dial_num_7" style="@style/dial_btn_style"/> <ImageButton android:id="@+id/eight" android:src="@drawable/dial_num_8" style="@style/dial_btn_style"/> <ImageButton android:id="@+id/nine" android:src="@drawable/dial_num_9" style="@style/dial_btn_style"/> <ImageButton android:id="@+id/star" android:src="@drawable/dial_num_star" style="@style/dial_btn_style"/> <ImageButton android:id="@+id/zero" android:src="@drawable/dial_num_0" style="@style/dial_btn_style"/> <ImageButton android:id="@+id/pound" android:src="@drawable/dial_num_pound" style="@style/dial_btn_style"/> </demo.phone.card.MyPhoneCard>
這樣,就實(shí)現(xiàn)了上圖的小鍵盤。這個例子參考Android自帶電話應(yīng)用的實(shí)現(xiàn)??梢?,在開發(fā)中,靈活運(yùn)用自定義的控件,可以實(shí)現(xiàn)獨(dú)特而富有魅力的效果!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解析android中隱藏與顯示軟鍵盤及不自動彈出鍵盤的實(shí)現(xiàn)方法
- Android 顯示和隱藏軟鍵盤的方法(手動)
- Android 設(shè)置Edittext獲取焦點(diǎn)并彈出軟鍵盤
- Android制作漂亮自適布局鍵盤的方法
- Android鍵盤顯示與隱藏代碼
- Android實(shí)現(xiàn)彈出鍵盤的方法
- Android中監(jiān)聽軟鍵盤顯示狀態(tài)實(shí)現(xiàn)代碼
- Android 軟鍵盤彈出時把原來布局頂上去的解決方法
- Android鍵盤輸入語言設(shè)置默認(rèn)打開myanmar緬甸語的步驟
- Android軟鍵盤遮擋的四種完美解決方案
相關(guān)文章
詳解Android中接口回調(diào)、方法回調(diào)
在Android開發(fā)中我們很多地方都用到了方法的回調(diào),回調(diào)就是把方法的定義和功能導(dǎo)入實(shí)現(xiàn)分開的一種機(jī)制,目的是為了解耦他的本質(zhì)是基于觀察者設(shè)計模式,即觀察者設(shè)計模式的的簡化版。本文主要對Android中接口回調(diào)、方法回調(diào)進(jìn)行詳細(xì)介紹,下面跟著小編一起來看下吧2017-01-01在Android打包中區(qū)分測試和正式環(huán)境淺析
這篇文章主要給大家介紹了關(guān)于在Android打包中如何區(qū)分測試和正式環(huán)境的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起看看吧。2017-10-10Android Activity與Fragment之間的跳轉(zhuǎn)實(shí)例詳解
這篇文章主要介紹了Android Activity與Fragment之間的跳轉(zhuǎn)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02手把手教你實(shí)現(xiàn)Android編譯期注解
今天給大家介紹Android編譯期注解sdk的步驟以及注意事項,并簡要分析了運(yùn)行時注解以及字節(jié)碼技術(shù)在生成代碼上與編譯期注解的不同與優(yōu)劣,感興趣的朋友一起看看吧2021-07-07Android 實(shí)現(xiàn)帶角標(biāo)的ImageView(微博,QQ消息提示)
下面小編就為大家分享一篇Android 實(shí)現(xiàn)帶角標(biāo)的ImageView(微博,QQ消息提示),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android視頻播放器屏幕左側(cè)邊隨手指上下滑動亮度調(diào)節(jié)功能的原理實(shí)現(xiàn)
這篇文章主要介紹了Android視頻播放器屏幕左側(cè)邊隨手指上下滑動亮度調(diào)節(jié)功能的原理實(shí)現(xiàn),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02Android 出現(xiàn)問題Installation error: INSTALL_FAILED_CONFLICTING_P
這篇文章主要介紹了Android 出現(xiàn)問題Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER解決辦法的相關(guān)資料,需要的朋友可以參考下2016-12-12Flutter中顯示條件Widget的實(shí)現(xiàn)方式
在 Flutter 日常開發(fā)中經(jīng)常會遇見這樣的需求,如: 只有用戶是 VIP 時,才能展示某個入口或者某個模塊,這樣的需求在開發(fā)業(yè)務(wù)需求中多如牛毛,那你是如何來優(yōu)雅的實(shí)現(xiàn)的呢,本文將給大家介紹Flutter中顯示條件Widget的實(shí)現(xiàn)方式,需要的朋友可以參考下2024-04-04Android 鍵盤開發(fā)知識點(diǎn)總結(jié)
這篇文章我們給大家總結(jié)了Android 鍵盤開發(fā)的相關(guān)知識點(diǎn)內(nèi)容以及開發(fā)心得,有需要的朋友參考學(xué)習(xí)下。2018-06-06Android設(shè)備藍(lán)牙連接掃描槍獲取掃描內(nèi)容
這篇文章主要為大家詳細(xì)介紹了Android設(shè)備藍(lán)牙連接掃描槍獲取掃描內(nèi)容,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09