当前位置: 移动技术网 > 移动技术>移动开发>Android > Android 自定义View 密码框实例代码

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

2019年07月24日  | 移动技术网移动技术  | 我要评论
暴露您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, 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);
}
} 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网