Android自定义控件之电话拨打小键盘
作者:chenjie19891104 发布时间:2022-11-17 21:52:08
标签:Android,电话拨打,键盘
关于Android的自定义控件,之前也写了两个,一个是简单地继承View,另一个通过继承Layout实现一个省市联动控件。这篇,将通过继承ViewGroup来实现一个电话拨打小键盘。本人一贯风格,懒得罗里吧嗦讲一大堆,直接上图上代码,一切尽在注释中!
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);
}
/**
* 当从xml将所有的控件都调入内存后,触发的动作
* 在这里获取控件的大小,并计算整个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之前调用。这个方面调用两次
*/
@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+"");
//重新计算后的结果,需要设置。下面这个方法必须调用
setMeasuredDimension(width, height);
}
/**
* 这个方法在onMeasure之后执行,这个自定义控件中含有12个子控件(每个小键),所以,重写这个方法,
* 调用每个键的layout,将他们一个一个布局好
* 就是4*3的放置,很简单,一个嵌套循环搞定
*/
@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;//这里其实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>
这样,就实现了上图的小键盘。这个例子参考Android自带电话应用的实现。可见,在开发中,灵活运用自定义的控件,可以实现独特而富有魅力的效果!
来源:https://blog.csdn.net/chenjie19891104/article/details/6796409


猜你喜欢
- 今天跟大家分享一个利用外部Jar包来实现Java操作CSV文件一.资源下载1.直接下载Jar包:javacsv-2.0.jar2.利用Mav
- 本文实例为大家分享了android实现倒计时动态圈的具体代码,供大家参考,具体内容如下效果是这样,没动图:布局:<LinearLayo
- 本文实例为大家分享了Android实现View滑动效果的具体代码,供大家参考,具体内容如下一、View的滑动简介View的滑动是Androi
- 2017年一直以来在公司负责爬虫项目相关工程,主要业务有预定、库存、在开发中也遇到很多问题,随手记录一下,后续会持续更新。chrome、fi
- 在日常工作中,我们往往只关注 Java 内存使用情况,这主要是因为 Java 内存分析相关的工具比较多。与之不同的是,图片内存分析的工具比较
- Java中Static关键字的一些用法详解1. Static 修饰类属性,因为静态成员变量可以通过类名+属性名调用,非静态成员变量不能通过类
- 什么是事件?用户对组件的一个操作,称之为一个事件。事件源:能够产生事件的GUI组件对象。事件处理方法:能够接受,解析和处理事件类对象,实现与
- Vector的基本介绍1.:Vector类的定义:public class Vector<E> ext
- 通过 SpringBoot 实现了表单下的文件上传,前后端分离情况下的文件上传。本案例不连接数据库,只做基本的文件上传操作。在 Spring
- 环境Linux版本:CentOS 6.5、Ubuntu 12.04.5 JDK版本:JDK 1.7目录方法一:手动解压JDK的压缩包,然后设
- 本文实例为大家分享了Java实现中英文词典功能的具体代码,供大家参考,具体内容如下功能如下:1、可以向词典中增加中英文单词,并提供修改和删除
- 本文将会介绍Jetpack Compose中的Modifier。在谷歌官方文档中它的描述是这么一句话:Modifier元素是一个有序、不可变
- 在Linux中创建一个新进程的唯一方法是使用fork()函数。fork()函数是Linux中一个非常重要的函数,和以往遇到的函数有一些区别,
- 这篇文章主要介绍了Springboot打包部署代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 本文研究的主要是java中的null“类型”的相关实例,具体介绍如下。先给出一道简单的null相关的题目,引发我们对null的探讨,后面会根
- 本文讨论了Spring Data JDBC如何实现DDD中聚合根存储的设计思路,其中主要讨论了是不是每个实体都需要一个对应数据表,这种问题需
- 1、反射的概念1、概念反射,指在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法,对任意一个对象,都能调用它的任意一个方法。这种
- 问题:1.线程 wait()方法使用有什么前提?2. 多线程之间如何进行通信?3. Java 中 notify 和 notifyAll 有什
- 本文实例讲述了Hibernate批量处理海量数据的方法。分享给大家供大家参考,具体如下:Hibernate批量处理海量其实从性能上考虑,它是
- 高快省的排序算法有没有既不浪费空间又可以快一点的排序算法呢?那就是“快速排序”啦!光听这个名字是不是就觉得很高端呢。假设我们现在对“6 1