Android 自定义View 密码框实例代码

Android 自定义View 密码框实例代码,第1张

概述暴露您view中所有影响可见外观的属性或者行为。•通过XML添加和设置样式

暴露您vIEw中所有影响可见外观的属性或者行为。

•通过XML添加和设置样式

•通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器

详细步骤见:Android 自定义View步骤

效果图展示:

支持的样式

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

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

<declare-styleable name="PasswordinputVIEw"><attr name="borderWIDth" format="dimension"/><attr name="bordercolor" format="color"/><attr name="borderRadius" format="dimension"/><attr name="passwordLength" format="integer"/><attr name="passworDWIDth" format="dimension"/><attr name="passwordcolor" format="color"/><attr name="passwordRadius" format="dimension"/></declare-styleable> 

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

绘制逻辑的主要代码

protected voID onDraw(Canvas canvas) {int wIDth = getWIDth();int height = getHeight();// 外边框RectF rect = new RectF(0,wIDth,height);borderPaint.setcolor(bordercolor);canvas.drawRoundRect(rect,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,borderPaint);// 分割线borderPaint.setcolor(bordercolor);borderPaint.setstrokeWIDth(defaultSplitlinewidth);for (int i = 1; i < passwordLength; i++) {float x = wIDth * i / passwordLength;canvas.drawline(x,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);}} 

总结

以上是内存溢出为你收集整理的Android 自定义View 密码框实例代码全部内容,希望文章能够帮你解决Android 自定义View 密码框实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1141198.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存