网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

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

本文名称:怎么在Android应用中自定义一个数字键盘与密码输入框-创新互联
本文路径:http://bjjierui.cn/article/dihieh.html

其他资讯