關(guān)于Android的自定義控件,之前也寫了兩個(gè),一個(gè)是簡(jiǎn)單地繼承View,另一個(gè)通過(guò)繼承Layout實(shí)現(xiàn)一個(gè)省市聯(lián)動(dòng)控件。這篇,將通過(guò)繼承ViewGroup來(lái)實(shí)現(xiàn)一個(gè)電話撥打小鍵盤。本人一貫風(fēng)格,懶得羅里吧嗦講一大堆,直接上圖上代碼,一切盡在注釋中!
創(chuàng)新互聯(lián)專業(yè)提供成都服務(wù)器托管服務(wù),為用戶提供五星數(shù)據(jù)中心、電信、雙線接入解決方案,用戶可自行在線購(gòu)買成都服務(wù)器托管服務(wù),并享受7*24小時(shí)金牌售后服務(wù)。

1、MyPhoneCard.java
/**
*
* 自定義一個(gè)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ā)的動(dòng)作
* 在這里獲取控件的大小,并計(jì)算整個(gè)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);
}
/**
* 這個(gè)方法在onFinishInflate之后,onLayout之前調(diào)用。這個(gè)方面調(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);//傳入我們希望得到的寬度,得到測(cè)量后的寬度
final int height = resolveSize(mHeight,heightMeasureSpec);//傳入我們希望得到的高度,得到測(cè)量后的高度
Log.v("ViewGroup Measured SIZE: width=", width+"");
Log.v("ViewGroup Measured SIZE: height=", height+"");
//重新計(jì)算后的結(jié)果,需要設(shè)置。下面這個(gè)方法必須調(diào)用
setMeasuredDimension(width, height);
}
/**
* 這個(gè)方法在onMeasure之后執(zhí)行,這個(gè)自定義控件中含有12個(gè)子控件(每個(gè)小鍵),所以,重寫這個(gè)方法,
* 調(diào)用每個(gè)鍵的layout,將他們一個(gè)一個(gè)布局好
* 就是4*3的放置,很簡(jiǎn)單,一個(gè)嵌套循環(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"
/>
<ImageButton android:id="@+id/two"
android:src="@drawable/dial_num_2"
/>
<ImageButton android:id="@+id/three"
android:src="@drawable/dial_num_3"
/>
<ImageButton android:id="@+id/four"
android:src="@drawable/dial_num_4"
/>
<ImageButton android:id="@+id/five"
android:src="@drawable/dial_num_5"
/>
<ImageButton android:id="@+id/six"
android:src="@drawable/dial_num_6"
/>
<ImageButton android:id="@+id/seven"
android:src="@drawable/dial_num_7"
/>
<ImageButton android:id="@+id/eight"
android:src="@drawable/dial_num_8"
/>
<ImageButton android:id="@+id/nine"
android:src="@drawable/dial_num_9"
/>
<ImageButton android:id="@+id/star"
android:src="@drawable/dial_num_star"
/>
<ImageButton android:id="@+id/zero"
android:src="@drawable/dial_num_0"
/>
<ImageButton android:id="@+id/pound"
android:src="@drawable/dial_num_pound"
/>
</demo.phone.card.MyPhoneCard> 這樣,就實(shí)現(xiàn)了上圖的小鍵盤。這個(gè)例子參考Android自帶電話應(yīng)用的實(shí)現(xiàn)??梢?jiàn),在開(kāi)發(fā)中,靈活運(yùn)用自定義的控件,可以實(shí)現(xiàn)獨(dú)特而富有魅力的效果!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)頁(yè)題目:Android自定義控件之電話撥打小鍵盤
當(dāng)前網(wǎng)址:http://www.yijiale78.com/article16/ghdhgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、虛擬主機(jī)、網(wǎng)站改版、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)