成都网站建设设计

将想法与焦点和您一起共享

怎么在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://chengdu.cdxwcx.cn/article/dihieh.html