欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义密码输入框和数字键盘

程序员文章站 2023-01-03 08:43:52
实现了一个自定义的密码输入框和自定义数字键盘,用作用户支付密码设置界面。先上效果图如下,方格样式,以及点击空白处隐藏软键盘。 控件实现清单: 1)集成于ed...

实现了一个自定义的密码输入框和自定义数字键盘,用作用户支付密码设置界面。先上效果图如下,方格样式,以及点击空白处隐藏软键盘。

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

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"?>
<keyboard xmlns:android="http://schemas.android.com/apk/res/android"
 android:horizontalgap="0px"
 android:keyheight="42dip"
 android:keywidth="31%p"
 android:verticalgap="0px" >
 
 <row>
  <key
   android:codes="49"
   android:keylabel="1" />
  <key
   android:codes="50"
   android:keylabel="2" />
  <key
   android:codes="51"
   android:keylabel="3" />
 </row>
 
 <row>
  <key
   android:codes="52"
   android:keylabel="4" />
  <key
   android:codes="53"
   android:keylabel="5" />
  <key
   android:codes="54"
   android:keylabel="6" />
 </row>
 
 <row>
  <key
   android:codes="55"
   android:keylabel="7" />
  <key
   android:codes="56"
   android:keylabel="8" />
  <key
   android:codes="57"
   android:keylabel="9" />
 </row>
 
 <row>
  <key
   android:codes="-3"
   android:keylabel="完成" />
  <key
   android:codes="48"
   android:keylabel="0" />
  <key
   android:codes="-5"
   android:keyicon="@drawable/sym_keyboard_delete" />
 </row>
 
</keyboard>

attrs.xml里面的样式:

<!-- 支付密码输入框 -->
 <declare-styleable name="passwordinputview">
  <attr name="borderwidth" format="dimension"/>
  <attr name="bordercolor" format="color"/>
  <attr name="borderradius" format="dimension"/>
  <attr name="passwordlength" format="integer"/>
  <attr name="passwordwidth" format="dimension"/>
  <attr name="passwordcolor" format="color"/>
  <attr name="passwordradius" format="dimension"/>
 </declare-styleable>

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/main_bg_color" >
 
 <include
  android:id="@+id/title_ll"
  layout="@layout/common_actionbar"/>
 
 <textview
  android:id="@+id/trader_pwd_set_tips_textview"
  style="@style/normal_text_style"
  android:layout_below="@+id/title_ll"
  android:layout_margintop="25dip"
  android:layout_centerhorizontal="true"
  android:text="@string/trade_pwd_set_tips_text" />
 
 <imageview
  android:id="@+id/line1_imageview"
  style="@style/line_horizontal_style"
  android:layout_below="@+id/trader_pwd_set_tips_textview"
  android:layout_margintop="26dip"
  android:contentdescription="@string/content_description" />
 
 <com.acoe.demo.widget.passwordinputview
  android:id="@+id/trader_pwd_set_pwd_edittext"
  android:layout_width="match_parent"
  android:layout_height="41dip"
  android:layout_below="@+id/line1_imageview"
  android:maxlength="6"
  android:background="@android:color/white" />
 
 <imageview
  android:id="@+id/line2_imageview"
  style="@style/line_horizontal_style"
  android:layout_below="@+id/trader_pwd_set_pwd_edittext"
  android:contentdescription="@string/content_description" />
 
 <button
  android:id="@+id/trader_pwd_set_next_button"
  style="@style/main_button_style"
  android:layout_below="@+id/line2_imageview"
  android:layout_margintop="25dip"
  android:text="@string/trade_pwd_set_next_text" />
 
 <android.inputmethodservice.keyboardview
  android:id="@+id/keyboard_view" 
  android:layout_width="match_parent" 
  android:layout_height="240dip"
  android:layout_alignparentbottom="true"
  android:paddingtop="30dip"
  android:paddingleft="13dip"
  android:paddingright="13dip"
  android:focusable="true" 
  android:focusableintouchmode="true" 
  android:visibility="invisible"/>
 
</relativelayout>

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。