package com.ddoctor.pro.common.view;
import java.util.ArrayList;
import java.util.List;
import com.ddoctor.pro.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.EditText;
/**
* 自定义手机输入框
* @author Administrator
*
*/
public class PhoneEditText extends EditText
{
/**
* 间隔
*/
private final int Phone_SPACING = 0;
/**
* 手机大小
*/
private final int Phone_SIZE = 50;
/**
* 手机号长度
*/
private final int Phone_LENGTH = 11;
/**
* 上下文
*/
private Context mContext;
/**
* 宽度
*/
private int mWidth;
/**
* 高度
*/
private int mHeight;
/**
* 手机框
*/
private Rect mRect;
/**
* 手机画笔
*/
private Paint mPhonePaint;
/**
* 手机框画笔
*/
private Paint mRectPaint;
/**
* 输入的手机长度
*/
private int mInputLength;
private int rectWidth;
private String s;
private List<String> list = new ArrayList<String>();
/**
* 输入结束监听
*/
private OnInputFinishListener mOnInputFinishListener;
/**
* 构造方法
*
* @param context
* @param attrs
*/
public PhoneEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
// 初始化手机画笔
mPhonePaint = new Paint();
mPhonePaint.setColor(Color.BLACK);
mPhonePaint.setStyle(Paint.Style.FILL);
mPhonePaint.setAntiAlias(true);
mPhonePaint.setTextSize(Phone_SIZE);
mPhonePaint.setTypeface(Typeface.DEFAULT);
mPhonePaint.setColor(context.getResources().getColor(R.color.color_text_gray_dark));
// 初始化手机框
mRectPaint = new Paint();
mRectPaint.setStyle(Paint.Style.STROKE);
mRectPaint.setColor(Color.LTGRAY);
mRectPaint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
mWidth = getWidth();
mHeight = getHeight();
// 这三行代码非常关键,大家可以注释点在看看效果
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawRect(0, 0, mWidth, mHeight, paint);
// 计算每个手机框宽度
rectWidth = (mWidth - Phone_SPACING * (Phone_LENGTH - 1)) / Phone_LENGTH;
// 绘制手机框
for (int i = 0; i < Phone_LENGTH; i++)
{
int left = (rectWidth + Phone_SPACING) * i;
int top = 2;
int right = left + rectWidth;
int bottom = mHeight - top;
mRect = new Rect(left, top, right, bottom);
canvas.drawRect(mRect, mRectPaint);
}
// 绘制手机号码
for (int i = 0; i < mInputLength; i++)
{
int cx = rectWidth / 2 + (rectWidth + Phone_SPACING) * i-10;
int cy = mHeight / 2+10;
canvas.drawText(list.get(i), cx, cy, mPhonePaint);
}
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter)
{
super.onTextChanged(text, start, lengthBefore, lengthAfter);
this.mInputLength = text.toString().length();
if (list != null && list.size() > 0)
{
list.clear();
}
for (int i = 0; i < mInputLength; i++)
{
list.add(text.toString().substring(i, i + 1));
}
invalidate();
if (mInputLength == Phone_LENGTH && mOnInputFinishListener != null)
{
mOnInputFinishListener.onInputFinish(text.toString());
}
}
public interface OnInputFinishListener
{
/**
* 手机输入结束监听
*
* @param password
*/
void onInputFinish(String phonenumber);
}
/**
* 设置输入完成监听
*
* @param onInputFinishListener
*/
public void setOnInputFinishListener(OnInputFinishListener onInputFinishListener)
{
this.mOnInputFinishListener = onInputFinishListener;
}
}