重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

如何实现Android自定义View验证码输入框

这篇文章主要讲解了如何实现Android自定义View验证码输入框,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

我们提供的服务有:做网站、网站建设、微信公众号开发、网站优化、网站认证、大方ssl等。为近1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的大方网站制作公司

验证码输入框

1.先看下样式

如何实现Android自定义View验证码输入框

2.直接上代码

public class MyVcode extends AppCompatEditText {
  private int mFigures = 0;// 验证码个数
  private int mCodeMargin = 0;// 验证码之间的间距
  private int mSelectColor = 0;// 选中框的颜色
  private int mNormalColor = 0;// 普通框的颜色
  private float mBorderWidth = 0f;// 边框的厚度

  Paint mNormalPaint = new Paint();
  Paint mSelectPaint = new Paint();
  Paint mOkContentPain = new Paint();//填写了内容的验证码

  private OnVerifyCodeChangedListener onVerifyCodeChangedListener;
  private int mCurrentPosition = 0;// 当前验证码的位置
  private int mEachRectLength = 0;// 矩形边长
  public MyVcode(Context context) {
    super(context, null);
  }


  public MyVcode(Context context, AttributeSet attrs) {
    super(context, attrs);
    initAttr(context,attrs);
    initPaint();
    setFocusableInTouchMode(true);
    initTextChangeListerner();
  }

  private void initTextChangeListerner() {
    addTextChangedListener(new TextWatcher() {
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        mCurrentPosition = getText().length();
      }

      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {
        mCurrentPosition = getText().length();
        postInvalidate();
        if (onVerifyCodeChangedListener!=null){
          onVerifyCodeChangedListener.onVerCodeChanged(getText().toString(),start,before,count);
        }
      }

      @Override
      public void afterTextChanged(Editable s) {
        mCurrentPosition = getText().length();
        postInvalidate();
        if (getText().length() == mFigures){
          if (onVerifyCodeChangedListener!=null){
            onVerifyCodeChangedListener.onInputCompleted(getText().toString());
          }
        }else if (getText().length()>mFigures){
          getText().delete(mFigures,getText().length());
        }
      }
    });
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction()==MotionEvent.ACTION_DOWN){
      requestFocus();
      setSelection(getText().length());
      showKeyBoard(getContext());
      return false;
    }

    return super.onTouchEvent(event);

  }

  private void showKeyBoard( Context context) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(this, InputMethodManager.SHOW_FORCED);
  }

  private void initAttr(Context context, AttributeSet attrs) {
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.VerifyCodeEditText);
    mFigures = ta.getInteger(R.styleable.VerifyCodeEditText_figures, 6);
    mCodeMargin = (int) ta.getDimension(R.styleable.VerifyCodeEditText_codeMargin, 0f);
    mSelectColor = ta.getColor(R.styleable.VerifyCodeEditText_selectBorderColor,getResources().getColor(R.color.base_barcolor));
    mNormalColor =
        ta.getColor(
            R.styleable.VerifyCodeEditText_normalBorderColor,
            getResources().getColor(android.R.color.darker_gray)
        );
    mBorderWidth = ta.getDimension(R.styleable.VerifyCodeEditText_borderWidth, 1f);

   /* mCursorColor =
        ta.getColor(
            R.styleable.VerifyCodeEditText_cursorColor,
            getResources().getColor(android.R.color.darker_gray)
        );
    ta.recycle();*/

   /* // force LTR because of bug: https://github.com/JustKiddingBaby/VercodeEditText/issues/4
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
      layoutDirection = LAYOUT_DIRECTION_LTR
    }*/
  }

  private void initPaint() {
    mNormalPaint.setAntiAlias(true);
    mNormalPaint.setColor(mNormalColor);
    mNormalPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mNormalPaint.setStrokeWidth(mBorderWidth);

    mSelectPaint.setAntiAlias(true);
    mSelectPaint.setColor(mSelectColor);
    mSelectPaint.setStyle(Paint.Style.STROKE);
    mSelectPaint.setStrokeWidth(mBorderWidth);


    mOkContentPain.setAntiAlias(true);
    mOkContentPain.setColor(mSelectColor);
    mOkContentPain.setStyle(Paint.Style.FILL_AND_STROKE);
    mOkContentPain.setStrokeWidth(mBorderWidth);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int size = MeasureSpec.getSize(heightMeasureSpec);
    // 每个矩形的宽度 相当于我门圆的直径
    mEachRectLength = size ;

  }

  @Override
  protected void onDraw(Canvas canvas) {
    //super.onDraw(canvas);
    Editable text = this.getText();
    mCurrentPosition = text.length();
    int width = mEachRectLength -getPaddingLeft() - getPaddingRight();

    for (int i =0 ;i< mFigures;i++){
      canvas.save();
      float start = width*i +mCodeMargin*i+mBorderWidth;
      float end = start+width - mBorderWidth;
      if (i==mFigures-1){
        end -= mBorderWidth;
      }
      RectF rect = new RectF(start, mBorderWidth, end, width - mBorderWidth);
      //画底色
      canvas.drawArc(rect, 0f, 360f, true, mNormalPaint);
      if (i == mCurrentPosition) {//选中的下一个状态
        canvas.drawArc(rect, 0f, 360f, true, mNormalPaint);
        canvas.drawArc(rect, 0f, 360f, true, mSelectPaint);
      }else {
        canvas.drawArc(rect, 0f, 360f, true, mNormalPaint);
      }

      if (mCurrentPosition>i){
        //画已经有内容的
        canvas.drawArc(rect, 0f, 360f, true, mOkContentPain);
      }
      canvas.restore();

    }
    String s = text.toString();

    //画文字
    for (int i = 0;i

3.自定义的属性


  
    
    
    
    
    
    

看完上述内容,是不是对如何实现Android自定义View验证码输入框有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。


网站题目:如何实现Android自定义View验证码输入框
本文URL:http://cqcxhl.cn/article/ppphso.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP