文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2023-05-31 01:41

关注

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

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 资料下载
  • 历年真题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯