Android仿滴滴出行验证码输入框功能实例代码

 更新时间:2017年12月18日 09:15:02   作者:hydCoder  
最近项目经理交给我们组一个类似滴滴出行填写验证码的弹框功能,拿到这个项目需求真是把我忙晕了,下面通过本文给大家分享Android仿滴滴出行验证码输入框功能实例代码,需要的朋友参考下吧

最近公司项目中有一个类似滴滴出行填写验证码的弹框,下面是我撸出来的效果:

 

中间的那个输入密码的6个框框其实就是用shape画的背景,通过监听EditText获取焦点来改变背景,废话少说,直接上代码吧。

2、效果实现

代码内容比较简单,所以大家可以直接看代码

VerificationCodeInput.java

/**
  * @author hydCoder
  * @date 2017/9/22 14:39
  * @desc 输入验证码的自定义view
  * @email hyd_coder@163.com
  */
public class VerificationCodeInput extends LinearLayout implements TextWatcher, View.OnKeyListener{
private final static String TYPE_NUMBER = "number";
private final static String TYPE_TEXT = "text";
private final static String TYPE_PASSWORD = "password";
private final static String TYPE_PHONE = "phone";
private static final String  TAG      = "VerificationCodeInput";
private       int   box      = 4;
private       int   boxWidth   = 80;
private       int   boxHeight   = 80;
private       int   childHPadding = 14;
private       int   childVPadding = 14;
private       String  inputType   = TYPE_NUMBER;
private       Drawable boxBgFocus  = null;
private       Drawable boxBgNormal  = null;
private Listener listener;
private boolean    focus      = false;
private List<EditText> mEditTextList  = new ArrayList<>();
private int      currentPosition = 0;
public VerificationCodeInput(Context context, AttributeSet attrs) {
  super(context, attrs);
  TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.vericationCodeInput);
  box = a.getInt(R.styleable.vericationCodeInput_box, 4);
  childHPadding = (int) a.getDimension(R.styleable.vericationCodeInput_child_h_padding, 0);
  childVPadding = (int) a.getDimension(R.styleable.vericationCodeInput_child_v_padding, 0);
  boxBgFocus = a.getDrawable(R.styleable.vericationCodeInput_box_bg_focus);
  boxBgNormal = a.getDrawable(R.styleable.vericationCodeInput_box_bg_normal);
  inputType = a.getString(R.styleable.vericationCodeInput_inputType);
  boxWidth = (int) a.getDimension(R.styleable.vericationCodeInput_child_width, boxWidth);
  boxHeight = (int) a.getDimension(R.styleable.vericationCodeInput_child_height, boxHeight);
  initViews();
}
@Override
protected void onAttachedToWindow() {
  super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
  super.onDetachedFromWindow();
}
private void initViews() {
  for (int i = 0; i < box; i++) {
    EditText editText = new EditText(getContext());
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(boxWidth, boxHeight);
    layoutParams.bottomMargin = childVPadding;
    layoutParams.topMargin = childVPadding;
    layoutParams.leftMargin = childHPadding;
    layoutParams.rightMargin = childHPadding;
    layoutParams.gravity = Gravity.CENTER;
    editText.setOnKeyListener(this);
    if(i == 0)
      setBg(editText, true);
    else setBg(editText, false);
    editText.setTextColor(Color.BLACK);
    editText.setLayoutParams(layoutParams);
    editText.setGravity(Gravity.CENTER);
    editText.setInputType(EditorInfo.TYPE_CLASS_PHONE);
    editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1)});
    if (TYPE_NUMBER.equals(inputType)) {
      editText.setInputType(InputType.TYPE_CLASS_NUMBER);
    } else if (TYPE_PASSWORD.equals(inputType)){
      editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
    } else if (TYPE_TEXT.equals(inputType)){
      editText.setInputType(InputType.TYPE_CLASS_TEXT);
    } else if (TYPE_PHONE.equals(inputType)){
      editText.setInputType(InputType.TYPE_CLASS_PHONE);
    }
    editText.setId(i);
    editText.setEms(1);
    editText.addTextChangedListener(this);
    addView(editText,i);
    mEditTextList.add(editText);
  }
}
private void backFocus() {
  int count = getChildCount();
  EditText editText ;
  for (int i = count-1; i>= 0; i--) {
    editText = (EditText) getChildAt(i);
    if (editText.getText().length() == 1) {
      editText.requestFocus();
      setBg(mEditTextList.get(i),true);
      //setBg(mEditTextList.get(i-1),true);
      editText.setSelection(1);
      return;
    }
  }
}
private void focus() {
  int count = getChildCount();
  EditText editText ;
  for (int i = 0; i< count; i++) {
    editText = (EditText) getChildAt(i);
    if (editText.getText().length() < 1) {
      editText.requestFocus();
      return;
    }
  }
}
private void setBg(EditText editText, boolean focus) {
  if (boxBgNormal != null && !focus) {
    editText.setBackground(boxBgNormal);
  } else if (boxBgFocus != null && focus) {
    editText.setBackground(boxBgFocus);
  }
}
private void setBg(){
  int count = getChildCount();
  EditText editText ;
  for(int i = 0; i< count; i++){
    editText = (EditText) getChildAt(i);
    if (boxBgNormal != null && !focus) {
      editText.setBackground(boxBgNormal);
    } else if (boxBgFocus != null && focus) {
      editText.setBackground(boxBgFocus);
    }
  }
}
private void checkAndCommit() {
  StringBuilder stringBuilder = new StringBuilder();
  boolean full = true;
  for (int i = 0 ;i < box; i++){
    EditText editText = (EditText) getChildAt(i);
    String content = editText.getText().toString();
    if ( content.length() == 0) {
      full = false;
      break;
    } else {
      stringBuilder.append(content);
    }
  }
  if (full){
    if (listener != null) {
      listener.onComplete(stringBuilder.toString());
      setEnabled(false);
    }
  }
}
@Override
public void setEnabled(boolean enabled) {
  int childCount = getChildCount();
  for (int i = 0; i < childCount; i++) {
    View child = getChildAt(i);
    child.setEnabled(enabled);
  }
}
public void setOnCompleteListener(Listener listener){
  this.listener = listener;
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
  return new LinearLayout.LayoutParams(getContext(), attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int count = getChildCount();
  for (int i = 0; i < count; i++) {
    View child = getChildAt(i);
    this.measureChild(child, widthMeasureSpec, heightMeasureSpec);
  }
  if (count > 0) {
    View child = getChildAt(0);
    int cHeight = child.getMeasuredHeight();
    int cWidth = child.getMeasuredWidth();
    int maxH = cHeight + 2 * childVPadding;
    int maxW = (cWidth + childHPadding) * box + childHPadding;
    setMeasuredDimension(resolveSize(maxW, widthMeasureSpec),
        resolveSize(maxH, heightMeasureSpec));
  }
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
  int childCount = getChildCount();
  for (int i = 0; i < childCount; i++) {
    View child = getChildAt(i);
    child.setVisibility(View.VISIBLE);
    int cWidth = child.getMeasuredWidth();
    int cHeight = child.getMeasuredHeight();
    int cl = (i) * (cWidth + childHPadding);
    int cr = cl + cWidth;
    int ct = childVPadding;
    int cb = ct + cHeight;
    child.layout(cl, ct, cr, cb);
  }
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
  if (start == 0 && count >= 1 && currentPosition != mEditTextList.size() - 1) {
    currentPosition++;
    mEditTextList.get(currentPosition).requestFocus();
    setBg(mEditTextList.get(currentPosition),true);
    setBg(mEditTextList.get(currentPosition-1),false);
  }
}
@Override
public void afterTextChanged(Editable s) {
  if (s.length() == 0) {
  } else {
    focus();
    checkAndCommit();
  }
}
@Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
  EditText editText = (EditText) view;
  if (keyCode == KeyEvent.KEYCODE_DEL && editText.getText().length() == 0) {
    int action = event.getAction();
    if (currentPosition != 0 && action == KeyEvent.ACTION_DOWN) {
      currentPosition--;
      mEditTextList.get(currentPosition).requestFocus();
      setBg(mEditTextList.get(currentPosition),true);
      setBg(mEditTextList.get(currentPosition+1),false);
      mEditTextList.get(currentPosition).setText("");
    }
  }
  return false;
}
public interface Listener {
  void onComplete(String content);
}
} ··· styles.xml里添加自定义属性
<declare-styleable name="vericationCodeInput">
  <attr name="box" format="integer" />
  <attr name="child_h_padding" format="dimension"/>
  <attr name="child_v_padding" format="dimension"/>
  <attr name="child_width" format="dimension"/>
  <attr name="child_height" format="dimension"/>
  <attr name="padding" format="dimension"/>
  <attr name="box_bg_focus" format="reference"/>
  <attr name="box_bg_normal" format="reference"/>
  <attr name="inputType" format="string"/>
</declare-styleable>

输入框获取焦点时的背景

verification_edit_bg_focus.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:radius="8dip" />
<stroke
  android:width="2dip"
  android:color="@color/auxiliary_color" />
</shape>

输入框没有获取焦点时的背景

verification_edit_bg_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="@color/white" />
  <corners android:radius="8dip" />
  <stroke
    android:width="1dip"
    android:color="@color/divide_color"/>
</shape>

在界面中使用

<com.sdalolo.genius.ui.view.VerificationCodeInput
  android:digits="1234567890"
  android:id="@+id/verificationCodeInput"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="25dp"
  android:layout_gravity="center_horizontal"
  ver:box="6"
  ver:box_bg_normal="@drawable/verification_edit_bg_normal"
  ver:box_bg_focus="@drawable/verification_edit_bg_focus"
  ver:child_h_padding="5dp"
  android:layout_centerInParent="true"
  android:layout_marginBottom="16dp"/>

然后对它设置输入完成后的监听

verificationCodeInput.setOnCompleteListener(new VerificationCodeInput.Listener() {
    @Override
    public void onComplete(String content) {
      btn_confirm.setEnabled(true);
      btn_confirm.setBackgroundResource(R.drawable.btn_bg_shape_enable);
      btn_confirm.setTextColor(Color.parseColor("#e4c16a"));
      codeNum = content;
    }
  });

总结

以上所述是小编给大家介绍的Android仿滴滴出行验证码输入框功能实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • Android提高之蓝牙隐藏API探秘

    Android提高之蓝牙隐藏API探秘

    这篇文章主要介绍了Android的蓝牙隐藏API功能,在Android项目开发中有一定的借鉴价值,需要的朋友可以参考下
    2014-08-08
  • android viewpaper实例探讨

    android viewpaper实例探讨

    本文将提供一个android viewpaper实例实现过程,需要了解更多的朋友可以参考下
    2012-12-12
  • Flutter实现笑嘻嘻的动态表情的示例代码

    Flutter实现笑嘻嘻的动态表情的示例代码

    这篇文章主要为大家介绍了如何利用Flutter实现笑嘻嘻的动态表情,文中的示例代码讲解详细,对我们学习Flutter有一定帮助,感兴趣的可以了解一下
    2022-04-04
  • Android VideoView类实例讲解

    Android VideoView类实例讲解

    本文主要介绍Android VideoView类,这里对VideoView类详细说明了使用方法,以及示例代码,有兴趣的朋友可以参考下,希望能帮助Android 开发的朋友
    2016-08-08
  • Kotlin中Object关键字的使用示例介绍

    Kotlin中Object关键字的使用示例介绍

    在Kotlin中object是一个特殊的关键字。主要用于声明一个类的同时创建这个类的对象(例如单例)。在Kotlin中它有三个方面的应用:对象表达式,对象声明和伴生对象
    2022-09-09
  • android监听返回按钮事件的方法

    android监听返回按钮事件的方法

    这篇文章主要介绍了android监听返回按钮事件的方法,涉及Android事件监听的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-10-10
  • Android实现弹出列表、单选、多选框

    Android实现弹出列表、单选、多选框

    这篇文章主要为大家详细介绍了Android实现弹出列表、单选、多选框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • Android 手机屏幕适配解决办法

    Android 手机屏幕适配解决办法

    这篇文章主要介绍了Android 手机屏幕适配的相关资料,在开发Android 手机开发的时候经常会有很多手机品牌和手机屏幕尺寸问题,需要的朋友可以参考下
    2016-10-10
  • 浅谈Android app开发中Fragment的Transaction操作

    浅谈Android app开发中Fragment的Transaction操作

    这篇文章主要介绍了Android app开发中Fragment的Transaction操作,包括Transaction和Fragment的生命周期的联系等内容,需要的朋友可以参考下
    2016-02-02
  • Android屏蔽软键盘自动弹出的解决方案

    Android屏蔽软键盘自动弹出的解决方案

    在编辑框输入内容时会弹出软键盘,而手机屏幕区域有限往往会遮住输入界面,怎么实现这种效果呢?下面小编给大家分享了Android屏蔽软键盘自动弹出的解决方案,需要的朋友参考下吧
    2017-01-01

最新评论