• [原] Android 自定义View 密码框 例子


    遵从准则

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

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

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

    样子

    S40620-155511

    支持的样式

    可以通过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);
    	}
    }	

    完整代码下载

    https://github.com/tianshaojie/Android-PasswordInputView

  • 相关阅读:
    js 数组操作常用方法
    如何成为一名卓越的前端工程师
    js实现日期显示的一些操作
    JavaScript 创建对象的七种方式
    ios微信浏览器音乐自动播放
    Mac下新建.开头的隐藏文件
    JavaScript判断浏览器UA 、 添加收藏 、设置首页 、调用本地邮箱发送邮件
    获取当前页面参数Hash和Search,或者当前Script的参数
    AJAX调用代码实例
    移动端兼容笔记整理
  • 原文地址:https://www.cnblogs.com/purediy/p/android_custom_view.html
Copyright © 2020-2023  润新知