设为首页 加入收藏

TOP

Android 自定义View 密码框 例子
2015-08-31 21:25:25 来源: 作者: 【 】 浏览:545
Tags:Android 定义 View 密码 例子

遵从准则


样子



支持的样式


可以通过XML定义影响外边和行为的属性如下


边框圆角值,边框颜色,分割线颜色,边框宽度,密码长度,密码大小,密码颜色



? ?


? ?


? ?


? ?


? ?


? ?


? ?



同时支持原来EditText功能,可以获得数据值,数字键盘设置等


绘制逻辑的主要代码


protected void onDraw(Canvas canvas) {


? ? int width = getWidth();


? ? int height = getHeight();


?


? ? // 外边框


? ? RectF rect = new RectF(0, 0, width, height);


? ? borderPaint.setColor(borderColor);


? ? canvas.drawRoundRect(rect, borderRadius, borderRadius, borderPaint);


?


? ? // 内容区


? ? RectF rectIn = new RectF(rect.left + defaultContMargin, rect.top + defaultContMargin,


? ? ? ? ? ? rect.right - defaultContMargin, rect.bottom - defaultContMargin);


? ? borderPaint.setColor(Color.WHITE);


? ? canvas.drawRoundRect(rectIn, borderRadius, borderRadius, borderPaint);


?


? ? // 分割线


? ? borderPaint.setColor(borderColor);


? ? borderPaint.setStrokeWidth(defaultSplitLineWidth);


? ? for (int i = 1; i < passwordLength; i++) {


? ? ? ? float x = width * i / passwordLength;


? ? ? ? canvas.drawLine(x, 0, x, height, borderPaint);


? ? }


?


? ? // 密码


? ? float cx, cy = height/ 2;


? ? float half = width / passwordLength / 2;


? ? for(int i = 0; i < textLength; i++) {


? ? ? ? cx = width * i / passwordLength + half;


? ? ? ? canvas.drawCircle(cx, cy, passwordWidth, passwordPaint);


? ? }


}?


完整代码下载


------------------------------------------分割线------------------------------------------


具体下载目录在 /2015年资料/8月/2日/Linux内核代码阅读心得体会PDF/


------------------------------------------分割线------------------------------------------


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android 自定义View步骤 下一篇JavaScript 基础工具清单

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: