最近公司项目中有一个类似滴滴出行填写验证码的d框,下面是我撸出来的效果:
中间的那个输入密码的6个框框其实就是用shape画的背景,通过监听EditText获取焦点来改变背景,废话少说,直接上代码吧。
2、效果实现
代码内容比较简单,所以大家可以直接看代码
VerificationCodeinput.java
/** * @author hydCoder * @date 2017/9/22 14:39 * @desc 输入验证码的自定义view * @email [email protected] */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();}@OverrIDeprotected voID onAttachedToWindow() { super.onAttachedToWindow();}@OverrIDeprotected 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); } }}@OverrIDepublic 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;}@OverrIDepublic LayoutParams generateLayoutParams(AttributeSet attrs) { return new linearLayout.LayoutParams(getContext(),attrs);}@OverrIDeprotected 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)); }}@OverrIDeprotected 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); }}@OverrIDepublic voID beforeTextChanged(CharSequence s,int start,int count,int after) {}@OverrIDepublic voID onTextChanged(CharSequence s,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); }}@OverrIDepublic voID afterTextChanged(Editable s) { if (s.length() == 0) { } else { focus(); checkAndCommit(); }}@OverrIDepublic 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仿滴滴出行验证码输入框功能实例代码全部内容,希望文章能够帮你解决Android仿滴滴出行验证码输入框功能实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)