Android 实现仿支付宝的密码均分输入框
程序员文章站
2022-06-04 21:40:39
android 仿支付宝的密码均分输入框
此为安卓项目,通过重绘edittext进行文字的均分排布。
直接贴上代码:
package com.xxx.xxx;...
android 仿支付宝的密码均分输入框
此为安卓项目,通过重绘edittext进行文字的均分排布。
直接贴上代码:
package com.xxx.xxx; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.rect; import android.text.editable; import android.text.selection; import android.text.textwatcher; import android.util.attributeset; import android.view.viewgroup; import android.widget.edittext; /** * 此控件为均分输入框控件 * 使用说明:xml文件中设置好文字大小,设置好宽度。高度使用wrap_content更佳,亦可设置固定高度 * (随着输入的行数变化会导致高度成倍增加) * 允许设置每行显示的文字个数 * 允许设置最多显示多少行 * 允许设置密码符显示 * 允许设置多行输入 * * created by yueer on 2015/10/22. */ public class exceleditview extends edittext { private int mmaxlength = 6; //一行显示的最大字符数 private int mcolorid = color.black; //字体颜色 private boolean ispassword = false; //是否需要显示密码符 private float mheight = 0.0f; //默认情况的高度 private int mmaxline = 0; //最大的行数:如果为0,---表示支持多行输入 不为0,--则为该行 public exceleditview(context context){ super(context); init(); } public exceleditview(context context, attributeset set){ super(context, set); init(); } private void init(){ this.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(charsequence s, int start, int before, int count) { // todo auto-generated method stub editable editable = exceleditview.this.gettext(); int len = editable.length(); if(mmaxline > 0 && len > mmaxlength*mmaxline) { int selendindex = selection.getselectionend(editable); string str = editable.tostring(); string newstr = str.substring(0,mmaxlength*mmaxline); exceleditview.this.settext(newstr); editable = exceleditview.this.gettext(); //新字符串的长度 int newlen = editable.length(); //旧光标位置超过字符串长度 if(selendindex > newlen) { selendindex = editable.length(); } //设置新光标所在的位置 selection.setselection(editable, selendindex); } } @override public void aftertextchanged(editable s) { } }); } public void setispassword(boolean ispassword){ this.ispassword = ispassword; } public void setmmaxline(int line){ this.mmaxline = line; } public void setmmaxlength(int leng){ this.mmaxlength = leng; } @override public void settextcolor(int color) { super.settextcolor(color); mcolorid = color; } @override protected void ondraw(canvas canvas) { char[] txt = this.gettext().tostring().tochararray(); //取出字符数组 int txtline = getlinefromchararray(txt); //计算有多少行 if (mmaxline > 0 && txtline > mmaxline){ //进行行数的上限处理 txtline = mmaxline; } if (this.ispassword){ //密码符的转义 for (int i=0; i<txt.length; i++){ txt[i] = '*'; } } if (mheight == 0){ //获取最初控件的高度 mheight = this.getheight(); } float width = this.getwidth(); float height = mheight * txtline; viewgroup.layoutparams params = this.getlayoutparams(); params.height = (int)height; this.setlayoutparams(params); //动态设置控件高度 float per = width / (mmaxlength+1); //宽度等分 float perheight = height / (txtline + 1); //高度等分 paint countpaint = new paint(paint.anti_alias_flag | paint.dev_kern_text_flag); countpaint.setcolor(mcolorid); countpaint.settextsize(this.gettextsize()); countpaint.settypeface(this.gettypeface()); countpaint.settextalign(paint.align.center); rect textbounds = new rect(); string numberstr = "1"; countpaint.gettextbounds(numberstr, 0, numberstr.length(), textbounds);//get text bounds, that can get the text width and height float textheight = (float)(textbounds.bottom - textbounds.top); float textwidth = (float)(textbounds.right = textbounds.left); //计算该控件中能够显示的单一文字的高度和宽度 for (int line = 0; line < txtline; line++) { for (int i = 0; i < mmaxlength && txt.length > (i+line*mmaxlength); i++) { canvas.drawtext(string.valueof(txt[i+line*mmaxlength]), (i + 1) * per - textwidth, perheight * (line + 1) + textheight / 2, countpaint); //进行绘制 } } } private int getlinefromchararray(char[] txt){ int line = ((txt.length - 1) / mmaxlength) + 1; return line; } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!