Android 自定义View 密码框实例代码
作者:雨知 时间:2022-08-22 17:06:02
暴露您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);
}
}
标签:android,自定义,view
0
投稿
猜你喜欢
java启动参数之谜的排查过程
2023-02-18 19:47:50
Eclipse设置断点调试的方法
2022-11-05 07:45:56
详解java模板和回调机制
2023-08-13 15:33:46
C#使用Monitor类实现线程同步
2021-07-20 03:01:14
java开发CPU流水线与指令乱序执行详解
2023-07-01 19:59:23
Java 10 局部变量类型推断浅析
2023-11-25 06:24:13
java连接ElasticSearch集群操作
2023-11-28 04:06:24
C#实现希尔排序
2023-11-02 08:15:04
在Android设备上搭建Web服务器的方法
2023-06-23 23:38:36
详解C#中的out和ref
2023-08-13 18:15:21
深入理解strcpy与memcpy的区别
2023-02-23 21:45:32
Java线程同步Lock同步锁代码示例
2023-11-17 13:23:54
UGUI绘制多点连续的平滑曲线
2022-01-16 06:22:45
java向文件中追加内容与读写文件内容源码实例代码
2021-11-15 11:45:13
华为鸿蒙系统应用开发工具 DevEco Studio的安装和使用图文教程
2022-09-14 05:04:23
Android RecyclerView的卡顿问题的解决方法
2023-07-01 20:01:14
详解Spring Boot的GenericApplicationContext使用教程
2021-06-09 14:05:40
详解Eclipse 字体、字号的设置、最佳字体推荐
2023-11-26 12:25:32
Java简单实现定时器
2023-07-16 18:10:58
mybatis输出SQL格式化方式
2021-06-18 18:19:45