返回顶部
首页 > 资讯 > 精选 >Android 实现仿支付宝的密码均分输入框
  • 710
分享到

Android 实现仿支付宝的密码均分输入框

android支付宝密码 2023-05-31 00:05:49 710人浏览 安东尼
摘要

Android 仿支付宝的密码均分输入框此为安卓项目,通过重绘edittext进行文字的均分排布。直接贴上代码:package com.xxx.xxx;import Android.content.Context;import androi

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

--结束END--

本文标题: Android 实现仿支付宝的密码均分输入框

本文链接: https://lsjlt.com/news/222471.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作