diff --git a/.gitignore b/.gitignore index 467f081..da50c3c 100644 --- a/.gitignore +++ b/.gitignore @@ -49,4 +49,4 @@ captures/ # Keystore files *.jks - +outcorelibrary diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/corelibrary/src/main/java/com/dalimao/corelibrary/VerificationCodeInput.java b/corelibrary/src/main/java/com/dalimao/corelibrary/VerificationCodeInput.java index fffc614..e950f36 100644 --- a/corelibrary/src/main/java/com/dalimao/corelibrary/VerificationCodeInput.java +++ b/corelibrary/src/main/java/com/dalimao/corelibrary/VerificationCodeInput.java @@ -18,6 +18,7 @@ import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.view.Gravity; +import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; @@ -35,7 +36,8 @@ public class VerificationCodeInput extends ViewGroup { private int boxHeight = 120; private int childHPadding = 14; private int childVPadding = 14; - private Drawable boxBg = null; + private Drawable boxBgFocus = null; + private Drawable boxBgNormal = null; private EditText currentFocusChild; private Listener listener; @@ -46,7 +48,8 @@ public class VerificationCodeInput extends ViewGroup { childHPadding = (int) a.getDimension(R.styleable.vericationCodeInput_child_h_padding, 0); childVPadding = (int) a.getDimension(R.styleable.vericationCodeInput_child_v_padding, 0); - boxBg = a.getDrawable(R.styleable.vericationCodeInput_box_bg); + boxBgFocus = a.getDrawable(R.styleable.vericationCodeInput_box_bg_focus); + boxBgNormal = a.getDrawable(R.styleable.vericationCodeInput_box_bg_normal); initViews(); } @@ -77,20 +80,54 @@ public class VerificationCodeInput extends ViewGroup { if (s.length() == 1) { EditText editText = (EditText) getChildAt((currentFocusChild.getId() + 1) % box); editText.requestFocus(); - currentFocusChild = editText; - - } } @Override public void afterTextChanged(Editable s) { - checkAndCommit(); + if (s.length() == 0) { + int id = currentFocusChild.getId(); + if (id > 0) { + EditText editText = (EditText) getChildAt((currentFocusChild.getId() - 1) % box); + editText.requestFocus(); + + } + } else { + checkAndCommit(); + } + } }; + OnFocusChangeListener listener = new OnFocusChangeListener() { + @Override + public void onFocusChange(View v, boolean hasFocus) { + + EditText editText = (EditText)v; + setBg(editText, hasFocus); + if (hasFocus) { + currentFocusChild = editText; + } + } + }; + + OnKeyListener onKeyListener = new OnKeyListener() { + @Override + public boolean onKey(View v, int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_DEL) { + EditText editText = (EditText) v; + int id = editText.getId(); + if (id > 0) { + EditText prEditText = (EditText) getChildAt((currentFocusChild.getId() - 1) % box); + prEditText.requestFocus(); + + } + } + return false; + } + }; for (int i = 0; i < box; i++) { @@ -101,11 +138,13 @@ public class VerificationCodeInput extends ViewGroup { layoutParams.leftMargin = childHPadding; layoutParams.rightMargin = childHPadding; layoutParams.gravity = Gravity.CENTER; - editText.setBackground(boxBg); + + editText.setOnFocusChangeListener(listener); + editText.setOnKeyListener(onKeyListener); + setBg(editText, false); editText.setLayoutParams(layoutParams); editText.setGravity(Gravity.CENTER); editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1)}); - editText.setCursorVisible(false); editText.setInputType(InputType.TYPE_CLASS_NUMBER); editText.setId(i); editText.setEms(1); @@ -121,6 +160,14 @@ public class VerificationCodeInput extends ViewGroup { } + private void setBg(EditText editText, boolean focus) { + 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; diff --git a/corelibrary/src/main/res/drawable/verification_edit_bg_focus.xml b/corelibrary/src/main/res/drawable/verification_edit_bg_focus.xml new file mode 100644 index 0000000..b85421f --- /dev/null +++ b/corelibrary/src/main/res/drawable/verification_edit_bg_focus.xml @@ -0,0 +1,11 @@ + + + + + + + + + diff --git a/corelibrary/src/main/res/drawable/verification_edit_bg_normal.xml b/corelibrary/src/main/res/drawable/verification_edit_bg_normal.xml new file mode 100644 index 0000000..d66d1ae --- /dev/null +++ b/corelibrary/src/main/res/drawable/verification_edit_bg_normal.xml @@ -0,0 +1,10 @@ + + + + + + + + diff --git a/corelibrary/src/main/res/drawable/verification_edit_bg_selector.xml b/corelibrary/src/main/res/drawable/verification_edit_bg_selector.xml deleted file mode 100644 index e3124c3..0000000 --- a/corelibrary/src/main/res/drawable/verification_edit_bg_selector.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/corelibrary/src/main/res/values/attrs.xml b/corelibrary/src/main/res/values/attrs.xml index 4f538a9..faf420d 100644 --- a/corelibrary/src/main/res/values/attrs.xml +++ b/corelibrary/src/main/res/values/attrs.xml @@ -6,7 +6,8 @@ - + + \ No newline at end of file diff --git a/simple/build.gradle b/simple/build.gradle index 2ef0050..0bc520c 100644 --- a/simple/build.gradle +++ b/simple/build.gradle @@ -26,6 +26,6 @@ dependencies { }) compile 'com.android.support:appcompat-v7:24.2.1' testCompile 'junit:junit:4.12' - // compile 'com.github.liuguangli:VerificationCodeInput:master-SNAPSHOT' + //compile 'com.github.liuguangli:VerificationCodeInput:master-SNAPSHOT' compile project(':corelibrary'); } diff --git a/simple/src/main/res/layout/activity_main.xml b/simple/src/main/res/layout/activity_main.xml index 8fd27f8..2fa25ff 100644 --- a/simple/src/main/res/layout/activity_main.xml +++ b/simple/src/main/res/layout/activity_main.xml @@ -1,5 +1,5 @@ - - + - - - + android:layout_marginBottom="16dp" + /> + + + + +