feat: 完善simple
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -49,4 +49,4 @@ captures/
|
|||||||
|
|
||||||
# Keystore files
|
# Keystore files
|
||||||
*.jks
|
*.jks
|
||||||
|
outcorelibrary
|
||||||
|
|||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@@ -18,6 +18,7 @@ import android.text.TextWatcher;
|
|||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Gravity;
|
import android.view.Gravity;
|
||||||
|
import android.view.KeyEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
@@ -35,7 +36,8 @@ public class VerificationCodeInput extends ViewGroup {
|
|||||||
private int boxHeight = 120;
|
private int boxHeight = 120;
|
||||||
private int childHPadding = 14;
|
private int childHPadding = 14;
|
||||||
private int childVPadding = 14;
|
private int childVPadding = 14;
|
||||||
private Drawable boxBg = null;
|
private Drawable boxBgFocus = null;
|
||||||
|
private Drawable boxBgNormal = null;
|
||||||
private EditText currentFocusChild;
|
private EditText currentFocusChild;
|
||||||
private Listener listener;
|
private Listener listener;
|
||||||
|
|
||||||
@@ -46,7 +48,8 @@ public class VerificationCodeInput extends ViewGroup {
|
|||||||
|
|
||||||
childHPadding = (int) a.getDimension(R.styleable.vericationCodeInput_child_h_padding, 0);
|
childHPadding = (int) a.getDimension(R.styleable.vericationCodeInput_child_h_padding, 0);
|
||||||
childVPadding = (int) a.getDimension(R.styleable.vericationCodeInput_child_v_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();
|
initViews();
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -77,20 +80,54 @@ public class VerificationCodeInput extends ViewGroup {
|
|||||||
if (s.length() == 1) {
|
if (s.length() == 1) {
|
||||||
EditText editText = (EditText) getChildAt((currentFocusChild.getId() + 1) % box);
|
EditText editText = (EditText) getChildAt((currentFocusChild.getId() + 1) % box);
|
||||||
editText.requestFocus();
|
editText.requestFocus();
|
||||||
currentFocusChild = editText;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterTextChanged(Editable s) {
|
public void afterTextChanged(Editable s) {
|
||||||
|
if (s.length() == 0) {
|
||||||
|
int id = currentFocusChild.getId();
|
||||||
|
if (id > 0) {
|
||||||
|
EditText editText = (EditText) getChildAt((currentFocusChild.getId() - 1) % box);
|
||||||
|
editText.requestFocus();
|
||||||
|
|
||||||
|
}
|
||||||
|
} else {
|
||||||
checkAndCommit();
|
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++) {
|
for (int i = 0; i < box; i++) {
|
||||||
@@ -101,11 +138,13 @@ public class VerificationCodeInput extends ViewGroup {
|
|||||||
layoutParams.leftMargin = childHPadding;
|
layoutParams.leftMargin = childHPadding;
|
||||||
layoutParams.rightMargin = childHPadding;
|
layoutParams.rightMargin = childHPadding;
|
||||||
layoutParams.gravity = Gravity.CENTER;
|
layoutParams.gravity = Gravity.CENTER;
|
||||||
editText.setBackground(boxBg);
|
|
||||||
|
editText.setOnFocusChangeListener(listener);
|
||||||
|
editText.setOnKeyListener(onKeyListener);
|
||||||
|
setBg(editText, false);
|
||||||
editText.setLayoutParams(layoutParams);
|
editText.setLayoutParams(layoutParams);
|
||||||
editText.setGravity(Gravity.CENTER);
|
editText.setGravity(Gravity.CENTER);
|
||||||
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1)});
|
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1)});
|
||||||
editText.setCursorVisible(false);
|
|
||||||
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
|
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
|
||||||
editText.setId(i);
|
editText.setId(i);
|
||||||
editText.setEms(1);
|
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() {
|
private void checkAndCommit() {
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
boolean full = true;
|
boolean full = true;
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
|
||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<solid android:color="#FFFFFF" />
|
||||||
|
<corners android:radius="3dip" />
|
||||||
|
<stroke
|
||||||
|
android:width="1dip"
|
||||||
|
android:color="#ffee33" />
|
||||||
|
</shape>
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
|
||||||
|
|
||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<solid android:color="#FaFafa" />
|
||||||
|
<corners android:radius="3dip" />
|
||||||
|
<stroke
|
||||||
|
android:width="1dip"
|
||||||
|
android:color="#BDC7D8" />
|
||||||
|
</shape>
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
|
||||||
<item android:state_window_focused="false">
|
|
||||||
<shape xmlns:android="http://schemas.android.com/apk/res/android">
|
|
||||||
<solid android:color="#FaFafa" />
|
|
||||||
<corners android:radius="3dip"/>
|
|
||||||
<stroke
|
|
||||||
android:width="1dip"
|
|
||||||
android:color="#BDC7D8" />
|
|
||||||
</shape>
|
|
||||||
</item>
|
|
||||||
<item android:state_focused="true" >
|
|
||||||
<shape xmlns:android="http://schemas.android.com/apk/res/android">
|
|
||||||
<solid android:color="#FFFFFF" />
|
|
||||||
<corners android:radius="3dip"/>
|
|
||||||
<stroke
|
|
||||||
android:width="1dip"
|
|
||||||
android:color="#ffee33" />
|
|
||||||
</shape>
|
|
||||||
</item>
|
|
||||||
</selector>
|
|
||||||
@@ -6,7 +6,8 @@
|
|||||||
<attr name="child_h_padding" format="dimension"/>
|
<attr name="child_h_padding" format="dimension"/>
|
||||||
<attr name="child_v_padding" format="dimension"/>
|
<attr name="child_v_padding" format="dimension"/>
|
||||||
<attr name="padding" format="dimension"/>
|
<attr name="padding" format="dimension"/>
|
||||||
<attr name="box_bg" format="reference"/>
|
<attr name="box_bg_focus" format="reference"/>
|
||||||
|
<attr name="box_bg_normal" format="reference"/>
|
||||||
</declare-styleable>
|
</declare-styleable>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
@@ -26,6 +26,6 @@ dependencies {
|
|||||||
})
|
})
|
||||||
compile 'com.android.support:appcompat-v7:24.2.1'
|
compile 'com.android.support:appcompat-v7:24.2.1'
|
||||||
testCompile 'junit:junit:4.12'
|
testCompile 'junit:junit:4.12'
|
||||||
// compile 'com.github.liuguangli:VerificationCodeInput:master-SNAPSHOT'
|
//compile 'com.github.liuguangli:VerificationCodeInput:master-SNAPSHOT'
|
||||||
compile project(':corelibrary');
|
compile project(':corelibrary');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
|
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
|
||||||
xmlns:ver="http://schemas.android.com/apk/res-auto"
|
xmlns:ver="http://schemas.android.com/apk/res-auto"
|
||||||
android:layout_width="match_parent" android:layout_height="match_parent"
|
android:layout_width="match_parent" android:layout_height="match_parent"
|
||||||
@@ -7,18 +7,53 @@
|
|||||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||||
android:paddingTop="@dimen/activity_vertical_margin"
|
android:paddingTop="@dimen/activity_vertical_margin"
|
||||||
|
android:orientation="vertical"
|
||||||
tools:context="com.dalimao.verificationcodeinput.MainActivity">
|
tools:context="com.dalimao.verificationcodeinput.MainActivity">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="系统样式:"
|
||||||
|
/>
|
||||||
<com.dalimao.corelibrary.VerificationCodeInput
|
<com.dalimao.corelibrary.VerificationCodeInput
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
ver:box="4"
|
ver:box="4"
|
||||||
ver:box_bg="@drawable/verification_edit_bg_selector"
|
|
||||||
ver:child_h_padding="5dp"
|
ver:child_h_padding="5dp"
|
||||||
android:layout_centerInParent="true"
|
android:layout_centerInParent="true"
|
||||||
android:id="@+id/verificationCodeInput"
|
android:id="@+id/verificationCodeInput"
|
||||||
>
|
android:layout_marginBottom="16dp"
|
||||||
|
/>
|
||||||
</com.dalimao.corelibrary.VerificationCodeInput>
|
<TextView
|
||||||
</RelativeLayout>
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="自动定义样式:"
|
||||||
|
android:padding="12dp"
|
||||||
|
/>
|
||||||
|
<com.dalimao.corelibrary.VerificationCodeInput
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
ver:box="4"
|
||||||
|
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"
|
||||||
|
/>
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="定义个数:"
|
||||||
|
android:padding="12dp"
|
||||||
|
/>
|
||||||
|
<com.dalimao.corelibrary.VerificationCodeInput
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
ver:box="5"
|
||||||
|
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"
|
||||||
|
/>
|
||||||
|
</LinearLayout>
|
||||||
|
|||||||
Reference in New Issue
Block a user