本文主要是介绍密码输入框(默认六位 [*] [*] [*] [*] [*] [*]),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/**
* 密码输入框(默认六位 [] [] [] [] [] [])
*
* @author xiely.
* @date 17/07/19.
*/
public class PasswordEditText extends EditText {private Context mContext;/*** 密码框输入间隔*/private float mSpacingWidth;/*** 默认密码位数*/private int mMaxCharact = 6;private Bitmap mPwdImg, inputLeft, inputMiddle, inputRight, inputBox;private Paint mPaint = new Paint();public PasswordEditText(Context context) {this(context, null);}public PasswordEditText(Context context, AttributeSet attrs) {super(context, attrs);init(context);}private void init(Context context) {this.mContext = context;mSpacingWidth = DimenUtils.dpToPx(12);setBackgroundColor(Color.TRANSPARENT);setMaxCharacter(mMaxCharact);setSingleLine(true);setCursorVisible(false);setLongClickable(false);setInputType(InputType.TYPE_CLASS_NUMBER);mPwdImg = BitmapFactory.decodeResource(context.getResources(), R.drawable.ui_pw_is_set);inputLeft = BitmapFactory.decodeResource(context.getResources(), R.drawable.ui_input_left);inputMiddle = BitmapFactory.decodeResource(context.getResources(), R.drawable.ui_input_middle);inputRight = BitmapFactory.decodeResource(context.getResources(), R.drawable.ui_input_right);inputBox = BitmapFactory.decodeResource(context.getResources(), R.drawable.ui_input_box);}public void setSpacingWidth(float mSpacingLen) {this.mSpacingWidth = DimenUtils.dpToPx(mSpacingLen);}public void setMaxCharacter(int maxCharacter) {this.mMaxCharact = maxCharacter;setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxCharacter)});}@Overrideprotected void onDraw(Canvas canvas) {float disWidth = getWidth();float disHeight = getHeight();int spacingCount = mMaxCharact - 1;//间隔数n-1float pwdWidth = (disWidth - mSpacingWidth * spacingCount) / 6;//密码框的宽度int textLen = getText().toString().trim().length();float cacheWidth = 0;boolean isHaveSpace = false;if (mSpacingWidth > 0) {isHaveSpace = true;}for (int i = 0; i < mMaxCharact; i++) {//绘制密码方格RectF rectRim = new RectF(cacheWidth, 0, cacheWidth + pwdWidth, disHeight);mPaint.setColor(Color.parseColor("#d3e0e3"));if (isHaveSpace) {canvas.drawBitmap(inputBox, null, rectRim, mPaint);} else {if (i == 0) {canvas.drawBitmap(inputLeft, null, rectRim, mPaint);} else if (i == mMaxCharact - 1) {canvas.drawBitmap(inputRight, null, rectRim, mPaint);} else {canvas.drawBitmap(inputMiddle, null, rectRim, mPaint);}}if (i < textLen) {mPaint.reset();canvas.drawBitmap(mPwdImg, pwdWidth / 2 - mPwdImg.getWidth() / 2 + cacheWidth,disHeight / 2 - mPwdImg.getHeight() / 2, mPaint);}cacheWidth += (pwdWidth + mSpacingWidth);}}
有什么好的想法和交流意见欢迎留言。。。
这篇关于密码输入框(默认六位 [*] [*] [*] [*] [*] [*])的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!