广告
返回顶部
首页 > 资讯 > 精选 >怎么在Android应用中自定义一个数字键盘与密码输入框
  • 430
分享到

怎么在Android应用中自定义一个数字键盘与密码输入框

androidroi 2023-05-31 01:05:29 430人浏览 薄情痞子
摘要

这期内容当中小编将会给大家带来有关怎么在Android应用中自定义一个数字键盘与密码输入框,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。控件实现清单: 1)集成于EditText的输入框控件:P

这期内容当中小编将会给大家带来有关怎么在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(); }}

--结束END--

本文标题: 怎么在Android应用中自定义一个数字键盘与密码输入框

本文链接: https://www.lsjlt.com/news/222770.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么在Android应用中自定义一个数字键盘与密码输入框
    这期内容当中小编将会给大家带来有关怎么在Android应用中自定义一个数字键盘与密码输入框,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。控件实现清单: 1)集成于EditText的输入框控件:P...
    99+
    2023-05-31
    android roi
  • Android中怎么自定义一个数字键盘
    这篇文章给大家介绍Android中怎么自定义一个数字键盘,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1. 实现键盘的 xml 布局网格样式的布局用 GridView 或者 RecyclerView 都可以实现,其实用...
    99+
    2023-05-30
    android
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作