资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

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

成都创新互联公司IDC提供业务:德阳服务器托管,成都服务器租用,德阳服务器托管,重庆服务器租用等四川省内主机托管与主机租用业务;数据中心含:双线机房,BGP机房,电信机房,移动机房,联通机房。

控件实现清单: 

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();
 }
}

NumKeyboardUtil 数字软键盘工具类

public class NumKeyboardUtil {
 private KeyboardView keyboardView; 
 private Keyboard k;// 数字键盘 
 private PasswordInputView ed;
 
 public NumKeyboardUtil(Activity act, Context ctx, PasswordInputView edit) { 
  this.ed = edit; 
  k = new Keyboard(ctx, R.xml.number); 
  keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view); 
  keyboardView.setKeyboard(k); 
  keyboardView.setEnabled(true); 
  keyboardView.setPreviewEnabled(true); 
  keyboardView.setOnKeyboardActionListener(listener); 
 }
 
 private OnKeyboardActionListener listener = new OnKeyboardActionListener() { 
  @Override 
  public void swipeUp() { 
  } 
 
  @Override 
  public void swipeRight() { 
  } 
 
  @Override 
  public void swipeLeft() { 
  } 
 
  @Override 
  public void swipeDown() { 
  } 
 
  @Override 
  public void onText(CharSequence text) { 
  } 
 
  @Override 
  public void onRelease(int primaryCode) { 
  } 
 
  @Override 
  public void onPress(int primaryCode) { 
  } 
  //一些特殊操作按键的codes是固定的比如完成、回退等 
  @Override 
  public void onKey(int primaryCode, int[] keyCodes) { 
    Editable editable = ed.getText(); 
    int start = ed.getSelectionStart(); 
    if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退 
      if (editable != null && editable.length() > 0) { 
        if (start > 0) { 
          editable.delete(start - 1, start); 
        } 
      } 
    }else if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
     hideKeyboard();
    } else { //将要输入的数字现在编辑框中 
      editable.insert(start, Character.toString((char) primaryCode)); 
    } 
  } 
 };
 
 public void showKeyboard() { 
  keyboardView.setVisibility(View.VISIBLE); 
 }
 
 public void hideKeyboard() {
  keyboardView.setVisibility(View.GONE);
 }
 
 public int getKeyboardVisible() {
  return keyboardView.getVisibility();
 }
}

number.xml
注意该文件需要放在项目下的res目录下的xml目录(没有就建个)里面

<?xml version="1.0" encoding="utf-8"?>

 
 
  
  
  
 
 
 
  
  
  
 
 
 
  
  
  
 
 
 
  
  
  
 
 

attrs.xml里面的样式:


 
  
  
  
  
  
  
  
 

布局代码:

<?xml version="1.0" encoding="utf-8"?>

 
 
 
 
 
 
 
 
 
 
 
 

Activity代码片段:

//=======在Activity成员变量中声明部分代码=======
/** 控件 */
 private PasswordInputView edtPwd;
 
//=======在Activity实例化控件部分代码=======
// 初始化控件
  edtPwd = (PasswordInputView) findViewById(R.id.trader_pwd_set_pwd_edittext);
  edtPwd.setInputType(InputType.TYPE_NULL); // 屏蔽系统软键盘
// 自定义软键盘
  if (keyboardUtil == null) keyboardUtil = new NumKeyboardUtil(this, this, edtPwd);
  edtPwd.setOnTouchListener(new OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    keyboardUtil.showKeyboard();
    return false;
   }
  });
 
//=======在Activity中重写onTouchEvent()方法,实现点击空白处隐藏软键盘=======
@Override
 public boolean onTouchEvent(MotionEvent event) {
  if(event.getAction() == MotionEvent.ACTION_DOWN){ 
    if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
     keyboardUtil.hideKeyboard();
    }
  }
  return super.onTouchEvent(event);
 }

ps:如果把该密码输入框和其他类型输入框并用时要注意两者之间焦点变化时将系统软键盘和自定义的数字键盘隐藏,我的做法是给密码输入框绑定OnFacusChangeListener事件,来控制就好。如下:

edtPwd.setOnFocusChangeListener(new OnFocusChangeListener() {
   @Override
   public void onFocusChange(View v, boolean hasFocus) {
    if (hasFocus) {
     // 如果系统键盘是弹出状态,先隐藏
     ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
     .hideSoftInputFromWindow(getCurrentFocus()
     .getWindowToken(),
     InputMethodManager.HIDE_NOT_ALWAYS);
     keyboardUtil.showKeyboard();
    } else {
     keyboardUtil.hideKeyboard();
    }
   }
  });

上述就是小编为大家分享的怎么在Android应用中自定义一个数字键盘与密码输入框了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


新闻名称:怎么在Android应用中自定义一个数字键盘与密码输入框
文章分享:http://www.cdkjz.cn/article/jdodce.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220