使用ViewGroup自定义数字键盘
首先看效果图
1.继承viewGroup实现GirdDialpad控件
package com.android.dialpad;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.util.Log;import android.view.View;import android.view.ViewGroup;public class GridDialPad extends ViewGroup{int row=0;int colum=0;private int mHeight;public GridDialPad(Context context) {super(context);// TODO Auto-generated constructor stub}public GridDialPad(Context context ,AttributeSet atts){super(context, atts);}public GridDialPad(Context context,AttributeSet attrs, int defstyle){super(context, attrs, defstyle);TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.GridDialPad);row=a.getInt(R.styleable.GridDialPad_row, 3);colum=a.getInt(R.styleable.GridDialPad_colum,3);Log.i("debug", "print"+"row="+row+",colum="+colum);a.recycle();}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {// TODO Auto-generated method stubint index=0;int y=(b-t)-mHeight+getPaddingTop();for (int i = 0; i < row; i++) {int x=getPaddingLeft();int btnHeight=getChildAt(index).getMeasuredHeight();for (int j = 0; j < colum; j++) {View child=getChildAt(index);int btnWidth=child.getMeasuredWidth();child.layout(x, y, x+btnWidth, y+btnHeight);x+=btnWidth;index++;}y+=btnHeight;}}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stub//super.onMeasure(widthMeasureSpec, heightMeasureSpec);int totalwidth=0;int totalheight=0;int index=0;for (int i = 0; i < row; i++) {for (int j = 0; j < colum; j++) { View view=getChildAt(index); view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);if(i==0){totalwidth+=view.getMeasuredHeight();}i++;}totalheight=getChildAt(index).getMeasuredHeight();}int width=resolveSize(totalwidth, widthMeasureSpec);int height=resolveSize(totalheight, heightMeasureSpec);mHeight=height;setMeasuredDimension(width, height);}}
2..layout布局中使用用自定义的GridDialPad
<?xml version="1.0" encoding="utf-8"?><com.android.dialpad.GridDialPad xmlns:android="http://schemas.android.com/apk/res/android" xmlns:dial="http://schemas.android.com/apk/res/com.android.dialpad" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" dial:row="3" dial:colum="3" > <ImageButton android:id="@+id/imageButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/dial_1" /> <ImageButton android:id="@+id/imageButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/dial_2" /> <ImageButton android:id="@+id/imageButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/dial_3" /> <ImageButton android:id="@+id/imageButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/dial_4" /> <ImageButton android:id="@+id/imageButton5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/dial_5" /> <ImageButton android:id="@+id/imageButton6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/dial_6" /> <ImageButton android:id="@+id/imageButton7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/dial_7" /> <ImageButton android:id="@+id/imageButton9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/dial_8" /> <ImageButton android:id="@+id/imageButton8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/dial_9" /></com.android.dialpad.GridDialPad >
3.界面布局文件mian.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="26dp" android:layout_marginTop="38dp" android:ems="10"> </EditText> <include layout="@layout/dialpad" android:layout_width="match_parent" android:layout_alignParentBottom="true" android:layout_height="wrap_content" /> </RelativeLayout>
4.Activity实现
package com.android.dialpad;import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.View.OnClickListener;import android.widget.EditText;public class NumberDialPadActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */EditText text; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text=(EditText)findViewById(R.id.editText1); setupDial(); } private void setupDial(){ findViewById(R.id.imageButton1).setOnClickListener(this); findViewById(R.id.imageButton2).setOnClickListener(this); findViewById(R.id.imageButton3).setOnClickListener(this); findViewById(R.id.imageButton4).setOnClickListener(this); findViewById(R.id.imageButton5).setOnClickListener(this); findViewById(R.id.imageButton6).setOnClickListener(this); findViewById(R.id.imageButton7).setOnClickListener(this); findViewById(R.id.imageButton8).setOnClickListener(this); findViewById(R.id.imageButton9).setOnClickListener(this); }public void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.imageButton1:keyPressed(KeyEvent.KEYCODE_1);break;case R.id.imageButton2:keyPressed(KeyEvent.KEYCODE_2);break;case R.id.imageButton3:keyPressed(KeyEvent.KEYCODE_3);break;case R.id.imageButton4:keyPressed(KeyEvent.KEYCODE_4);break;case R.id.imageButton5:keyPressed(KeyEvent.KEYCODE_5);break;case R.id.imageButton6:keyPressed(KeyEvent.KEYCODE_6);break;case R.id.imageButton7:keyPressed(KeyEvent.KEYCODE_7);break;case R.id.imageButton8:keyPressed(KeyEvent.KEYCODE_8);break;case R.id.imageButton9:keyPressed(KeyEvent.KEYCODE_9);break;default:break;}}private void keyPressed(int keyCode){KeyEvent event=new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);text.onKeyDown(keyCode, event);}}
其它文件
<?xml version="1.0" encoding="utf-8"?><resources> <!-- 自定属性 --> <declare-styleable name="GridDialPad"> <attr name="row" format="integer" /> <attr name="colum" format="integer" /> </declare-styleable> </resources>
代码下载http://download.csdn.net/detail/androidchuxueze/4467439