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

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

发布时间:2020-09-26 16:58:24

来源:脚本之家

阅读:114

作者:lqh

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

}

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值