Android自定义View模仿密码输入框

自定义密码输入view继承EditText

package com.anyan.www.ann;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.EditText;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;

public class PasswordInputView extends EditText {
    //外边框颜色
    private int borderColor = 0xFFCCCCCC;
    //外边框线的粗细
    private float borderWidth = 5;
    //外边框圆角半径
    private float borderRadius = 3;
    //中间分割线粗细
    private float dividerWidth = 3;
    //密码长度,默认6个字符
    private int passwordLength = 6;
    //密码文字颜色
    private int passwordColor = 0xFFCCCCCC;
    //密码圆点的半径
    private float passwordRadius = 3;
    //画笔
    private Paint paint;
    //整个view的宽,高
    private int width, height;

    public PasswordInputView(Context context, AttributeSet attrs) {
        super(context, attrs);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        borderWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, borderWidth, dm);
        borderRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, borderRadius, dm);
        passwordLength = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, passwordLength, dm);
        passwordRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 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);
        passwordRadius = a.getDimension(R.styleable.PasswordInputView_pivPasswordRadius, passwordRadius);
        dividerWidth = a.getDimension(R.styleable.PasswordInputView_dividerWidth, dividerWidth);
        a.recycle();

        paint = new Paint(ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        width = getWidth();
        height = getHeight();
        drawBorder(canvas);
        drawDivider(canvas);
        drawCircle(canvas);

    }

    //绘制外边框
    private void drawBorder(Canvas canvas) {
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(borderColor);
        paint.setStrokeWidth(borderWidth);
        RectF rectF = new RectF(0, 0, width, height);
        canvas.drawRoundRect(rectF, borderRadius, borderRadius, paint);
    }

    //绘制分割线
    private void drawDivider(Canvas canvas) {
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(borderColor);
        paint.setStrokeWidth(dividerWidth);
        for (int i = 1; i < passwordLength; i++) {
            int x = i * (width / passwordLength);
            canvas.drawLine(x, 0, x, height, paint);
        }
    }

    //绘制圆点
    private void drawCircle(Canvas canvas) {
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(passwordColor);
        String content = getText().toString().trim();
        for (int i = 0; i < content.length(); i++) {
            int cx = width / passwordLength / 2 + i * width / passwordLength;
            int cy = height / 2;
            canvas.drawCircle(cx, cy, width / passwordLength / 8, paint);
        }
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        invalidate();
        //密码输入完成后做的操作,例如密码验证,页面跳转等
        if (text.length() == passwordLength) {
            if (onCompleteListener != null) {
                onCompleteListener.onComplete();
            }
        }
    }

    private OnCompleteListener onCompleteListener;

    public void setOnCompleteListener(OnCompleteListener onCompleteListener) {
        this.onCompleteListener = onCompleteListener;
    }

    public interface OnCompleteListener {
        void onComplete();
    }
}
自定义数字属性:

  <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"/>
        <attr name="dividerWidth" format="dimension"/>
    </declare-styleable>

2.应用:

布局文件:

  <com.anyan.www.ann.PasswordInputView
        android:id="@+id/passwordInputView"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_margin="10dp"
        android:cursorVisible="false"
        android:inputType="number"
        app:pivBorderColor="#117bed"
        app:pivBorderRadius="5dp"
        app:pivBorderWidth="1dp"
        app:pivPasswordColor="#117bed"
        app:dividerWidth="0.5dp"
        android:maxLength="6"
        app:pivPasswordLength="6"
        android:background="@null"
        android:focusable="true" />

activity中代码:

public class MainActivity extends AppCompatActivity implements PasswordInputView.OnCompleteListener {


    private PasswordInputView passwordInputView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        passwordInputView = (PasswordInputView) findViewById(R.id.passwordInputView);
        passwordInputView.setOnCompleteListener(this);
    }

    @Override
    public void onComplete() {
        ToastUtil.showText(this, "密码输入完成");
    }

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值