自定义密码框,格子

    xiaoxiao2021-09-08  98

    安全中心 ——支付密码

    产品说非要改改改,于是搜了个,看了下逻辑也简单,再加上顺手改了改回调,于是传上来给看看

    上年纪的程序员不想说话,贴代码

    继承EdiText,根据passwordLength 平分出每个字符框,画格子。 每次onTextChanged在格子里画黑点。

    public class PasswordInputView extends EditText { private static final int defaultContMargin = 5; private static final int defaultSplitLineWidth = 3; private String tempInput; public void clearFirstInput(){ tempInput = ""; } public static interface InputListener { void firstInput(); void comfirm(String pwd); void comfirmError(); } public void setInputListener(final InputListener inputListener) { this.addTextChangedListener(new AfterTextWather() { @Override public void afterTextChanged(Editable s) { if (getText().length() < passwordLength) { return; } if (StringUtil.isEmpty(tempInput)) { tempInput = getText().toString(); inputListener.firstInput(); } else if (tempInput.equals(getText().toString())) { inputListener.comfirm(tempInput); } else { inputListener.comfirmError(); } } }); } private int borderColor = 0xFF333333; private float borderWidth = 3; private float borderRadius = 3; private int passwordLength = 6; private int passwordColor = 0x6666666; private float passwordWidth = 4; private float passwordRadius = 3; private Paint passwordPaint = new Paint(ANTI_ALIAS_FLAG); private Paint borderPaint = new Paint(ANTI_ALIAS_FLAG); private int textLength; public PasswordInputView(Context context, AttributeSet attrs) { super(context, attrs); DisplayMetrics dm = getResources().getDisplayMetrics(); borderWidth = (int) borderWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, borderWidth, dm); passwordWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, passwordWidth, dm); borderRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, borderRadius, dm); passwordRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, passwordRadius, dm); TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PasswordInputView, 0, 0); borderColor = a.getColor(R.styleable.PasswordInputView_pivBorderColor, borderColor); borderWidth = a.getDimension(R.styleable.PasswordInputView_pivBorderWidth, borderWidth); borderRadius = a.getDimension(R.styleable.PasswordInputView_pivBorderRadius, borderRadius); passwordLength = a.getInt(R.styleable.PasswordInputView_pivPasswordLength, passwordLength); passwordColor = a.getColor(R.styleable.PasswordInputView_pivPasswordColor, passwordColor); passwordWidth = a.getDimension(R.styleable.PasswordInputView_pivPasswordWidth, passwordWidth); passwordRadius = a.getDimension(R.styleable.PasswordInputView_pivPasswordRadius, passwordRadius); a.recycle(); borderPaint.setStrokeWidth(borderWidth); borderPaint.setColor(borderColor); passwordPaint.setStrokeWidth(passwordWidth); passwordPaint.setStyle(Paint.Style.FILL); passwordPaint.setColor(passwordColor); } @Override protected void onDraw(Canvas canvas) { int width = getWidth(); int height = getHeight(); // 外边框 RectF rect = new RectF(0, 0, width, height); borderPaint.setColor(borderColor); canvas.drawRoundRect(rect, borderRadius, borderRadius, borderPaint); // 内容区 RectF rectIn = new RectF(rect.left + defaultContMargin, rect.top + defaultContMargin, rect.right - defaultContMargin, rect.bottom - defaultContMargin); borderPaint.setColor(Color.WHITE); canvas.drawRoundRect(rectIn, borderRadius, borderRadius, borderPaint); // 分割线 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(); } }

    layout 引用

    <com.widget.keyboard.PasswordInputView android:id="@+id/txn_set_pwd_text" android:layout_below="@id/txn_set_pwd_lable" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="25dp" android:cursorVisible="false" android:layout_gravity="center_horizontal" android:inputType="number" android:layout_marginLeft="20dp" android:background="@color/white" android:layout_marginRight="20dp" android:maxLength="6" app:pivBorderColor="@color/color_useless_gray" app:pivBorderRadius="2dp" app:pivBorderWidth="1dp" app:pivPasswordColor="@color/color_3" app:pivPasswordLength="6" app:pivPasswordWidth="10dp" />

    attr.xml

    <!--密码输入框--> <declare-styleable name="PasswordInputView"> <attr name="pivBorderColor" format="color"/> <attr name="pivBorderWidth" format="dimension"/> <attr name="pivBorderRadius" format="dimension"/> <attr name="pivPasswordColor" format="color"/> <attr name="pivPasswordWidth" format="dimension"/> <attr name="pivPasswordRadius" format="dimension"/> <attr name="pivPasswordLength" format="integer"/> </declare-styleable>

    组件还可以优化,只是本人较懒,满足需求就暂时不动了。

    记录下来回头方便复制。自用勿怪~~

    转载请注明原文地址: https://ju.6miu.com/read-677502.html

    最新回复(0)