这期内容当中小编将会给大家带来有关怎么在Android应用中自定义一个数字键盘与密码输入框,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
控件实现清单:
1)集成于EditText的输入框控件:PasswordInputView.java
2)数字键盘工具类:NumKeyboardUtil.java
3)xml文件:number.xml
4)attrs样式
5)layout文件
具体内容:
PasswordInputView.java
public class PasswordInputView extends EditText{ private int textLength; private int borderColor; private float borderWidth; private float borderRadius; private int passwordLength; private int passwordColor; private float passwordWidth; private float passwordRadius; private Paint passwordPaint = new Paint(Paint.ANTI_ALIAS_FLAG); private Paint borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG); private final int defaultSplitLineWidth = 1; public PasswordInputView(Context context, AttributeSet attrs) { super(context, attrs); final Resources res = getResources(); final int defaultBorderColor = res.getColor(R.color.line_color); final float defaultBorderWidth = res.getDimension(R.dimen.dimen_1px); final float defaultBorderRadius = res.getDimension(R.dimen.dimen_6); final int defaultPasswordLength = 6; final int defaultPasswordColor = res.getColor(R.color.normal_text_color); final float defaultPasswordWidth = res.getDimension(R.dimen.dimen_6); final float defaultPasswordRadius = res.getDimension(R.dimen.dimen_6); TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PasswordInputView, 0, 0); try { borderColor = a.getColor(R.styleable.PasswordInputView_borderColor, defaultBorderColor); borderWidth = a.getDimension(R.styleable.PasswordInputView_borderWidth, defaultBorderWidth); borderRadius = a.getDimension(R.styleable.PasswordInputView_borderRadius, defaultBorderRadius); passwordLength = a.getInt(R.styleable.PasswordInputView_passwordLength, defaultPasswordLength); passwordColor = a.getColor(R.styleable.PasswordInputView_passwordColor, defaultPasswordColor); passwordWidth = a.getDimension(R.styleable.PasswordInputView_passwordWidth, defaultPasswordWidth); passwordRadius = a.getDimension(R.styleable.PasswordInputView_passwordRadius, defaultPasswordRadius); } finally { a.recycle(); } borderPaint.setStrokeWidth(borderWidth); borderPaint.setColor(borderColor); passwordPaint.setStrokeWidth(passwordWidth); passwordPaint.setStyle(Paint.Style.FILL); passwordPaint.setColor(passwordColor); setSingleLine(true); } @Override protected void onDraw(Canvas canvas) { int width = getWidth(); int height = getHeight(); // 分割线 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); } } @Override protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { super.onTextChanged(text, start, lengthBefore, lengthAfter); this.textLength = text.toString().length(); invalidate(); } public int getBorderColor() { return borderColor; } public void setBorderColor(int borderColor) { this.borderColor = borderColor; borderPaint.setColor(borderColor); invalidate(); } public float getBorderWidth() { return borderWidth; } public void setBorderWidth(float borderWidth) { this.borderWidth = borderWidth; borderPaint.setStrokeWidth(borderWidth); invalidate(); } public float getBorderRadius() { return borderRadius; } public void setBorderRadius(float borderRadius) { this.borderRadius = borderRadius; invalidate(); } public int getPasswordLength() { return passwordLength; } public void setPasswordLength(int passwordLength) { this.passwordLength = passwordLength; invalidate(); } public int getPasswordColor() { return passwordColor; } public void setPasswordColor(int passwordColor) { this.passwordColor = passwordColor; passwordPaint.setColor(passwordColor); invalidate(); } public float getPasswordWidth() { return passwordWidth; } public void setPasswordWidth(float passwordWidth) { this.passwordWidth = passwordWidth; passwordPaint.setStrokeWidth(passwordWidth); invalidate(); } public float getPasswordRadius() { return passwordRadius; } public void setPasswordRadius(float passwordRadius) { this.passwordRadius = passwordRadius; invalidate(); }}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1148
183.71 KB下载数642
644.84 KB下载数2756
相关文章
发现更多好内容- 如何对 Java 中的 Stack 类性能进行优化?(如何优化Java中Stack类的性能)
- Java 中运算符与 C++ 相比有哪些差异?(java中运算符对比C++有何不同 )
- 在 Java 中如何通过反射获取字段的值?(java中怎么通过反射获取字段的值)
- 如何在 Java 中创建类的实例对象?(java怎么创建类的实例对象)
- 如何高效更新Uncomtrade数据库数据
- 如何在 Java 中获取端口数据?(java怎么获取端口数据)
- 如何在 Java 中实现异步处理?(java怎么实现异步处理)
- Java 写小程序都用到了哪些技术呢?(java写小程序用到的技术有哪些)
- Java 动态数据源切换的方法究竟有哪些?(java动态数据源切换的方法是什么)
- JavaScript函数式编程的亮点有哪些?(JavaScript函数式编程有啥亮点)
猜你喜欢
AI推送时光机怎么在Android应用中自定义一个数字键盘与密码输入框
后端开发2023-05-31
Android中怎么自定义一个数字键盘
后端开发2023-05-30
咦!没有更多了?去看看其它编程学习网 内容吧