├── .gitignore
├── .idea
├── caches
│ └── build_file_checksums.ser
├── codeStyles
│ └── Project.xml
├── encodings.xml
├── gradle.xml
├── misc.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── xylitolz
│ │ └── androidverificationcode
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── xylitolz
│ │ │ └── androidverificationcode
│ │ │ ├── MainActivity.java
│ │ │ └── view
│ │ │ ├── VerificationCodeView.java
│ │ │ └── WiseEditText.java
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ ├── bg_text_focused.xml
│ │ ├── bg_text_normal.xml
│ │ ├── cursor.xml
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── attrs.xml
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── xylitolz
│ └── androidverificationcode
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── image
└── 自定义View-验证码仿滴滴.gif
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/libraries
5 | /.idea/modules.xml
6 | /.idea/workspace.xml
7 | .DS_Store
8 | /build
9 | /captures
10 | .externalNativeBuild
11 |
--------------------------------------------------------------------------------
/.idea/caches/build_file_checksums.ser:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/riceeater/AndroidVerificationCode/c05876dfe5a484a68f90accd72cdb00bb49db928/.idea/caches/build_file_checksums.ser
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
17 |
18 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 类滴滴验证码输入框,长这样:
2 |
3 | 
4 |
5 |
6 |
7 | ### 实现思路
8 |
9 | **笔者这里决定继承RelativeLayout来实现自定义验证码的功能。显示验证码可以使用几个TextView,这里需要将TextView统一管理,所以还需要一个TextView数组。有光标,那自然而然的就想到了EditText,可以使用一个透明背景的EditText。几个验证码可以使用TextView以一定的规则进行排列,通过监听EditText的输入,拦截到输入字符,并将字符传递给TextView数组,并将EditText置为空,同时重设TextView选中状态,移动EditText光标。笔者这里采用给EditText设置paddingLeft的方式来实现光标的移动,当然,需要经过一些计算。OK,大概思路就是这样了,具体的代码我们继续看下面的。**
10 |
11 | ### 使用:
12 |
13 | ```xml
14 |
25 | ```
26 |
27 |
28 |
29 | 效果参考上图
30 |
31 |
32 |
33 | 相关博客地址:[Android自定义View-仿滴滴自定义验证码输入框](https://riceeater.gitee.io/post/23-VerificationCodeView)
34 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | defaultConfig {
6 | applicationId "com.xylitolz.androidverificationcode"
7 | minSdkVersion 14
8 | targetSdkVersion 27
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(dir: 'libs', include: ['*.jar'])
23 | implementation 'com.android.support:appcompat-v7:27.1.1'
24 | implementation 'com.android.support.constraint:constraint-layout:1.1.0'
25 | testImplementation 'junit:junit:4.12'
26 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
28 | }
29 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/xylitolz/androidverificationcode/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.xylitolz.androidverificationcode;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.xylitolz.androidverificationcode", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xylitolz/androidverificationcode/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.xylitolz.androidverificationcode;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.widget.Button;
7 | import android.widget.Toast;
8 |
9 | import com.xylitolz.androidverificationcode.view.VerificationCodeView;
10 |
11 | public class MainActivity extends AppCompatActivity implements View.OnClickListener{
12 | private VerificationCodeView viewVerification;
13 | private Button btnSubmit;
14 | private Button btnClear;
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | setContentView(R.layout.activity_main);
19 | viewVerification = findViewById(R.id.view_verification);
20 | btnSubmit = findViewById(R.id.btn_submit);
21 | btnClear = findViewById(R.id.btn_clear);
22 |
23 | btnSubmit.setOnClickListener(this);
24 | btnClear.setOnClickListener(this);
25 | }
26 |
27 |
28 | @Override
29 | public void onClick(View v) {
30 | switch (v.getId()) {
31 | case R.id.btn_submit:
32 | if(viewVerification.isFinish()) {
33 | Toast.makeText(this,"输入验证码是:"+viewVerification.getContent(),Toast.LENGTH_SHORT).show();
34 | } else {
35 | Toast.makeText(this,"请输入完整验证码",Toast.LENGTH_SHORT).show();
36 | }
37 | break;
38 | case R.id.btn_clear:
39 | viewVerification.clear();
40 | break;
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xylitolz/androidverificationcode/view/VerificationCodeView.java:
--------------------------------------------------------------------------------
1 | package com.xylitolz.androidverificationcode.view;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Color;
6 | import android.graphics.drawable.Drawable;
7 | import android.support.annotation.DrawableRes;
8 | import android.text.Editable;
9 | import android.text.InputType;
10 | import android.text.TextUtils;
11 | import android.text.TextWatcher;
12 | import android.util.AttributeSet;
13 | import android.util.TypedValue;
14 | import android.view.Gravity;
15 | import android.view.KeyEvent;
16 | import android.view.View;
17 | import android.view.ViewGroup;
18 | import android.widget.RelativeLayout;
19 | import android.widget.TextView;
20 |
21 | import com.xylitolz.androidverificationcode.R;
22 |
23 | /**
24 | * @author 小米Xylitol
25 | * @email xiaomi987@hotmail.com
26 | * @desc Android验证码View
27 | * @date 2018-05-09 10:25
28 | */
29 | public class VerificationCodeView extends RelativeLayout {
30 |
31 | //当前验证码View用来展示验证码的TextView数组,数组个数由codeNum决定
32 | private TextView[] textViews;
33 | //用来输入验证码的EditText输入框,输入框会跟随输入个数移动,显示或者隐藏光标等
34 | private WiseEditText editText;
35 | //当前验证码View展示验证码个数
36 | private int codeNum;
37 | //每个TextView的宽度
38 | private float codeWidth;
39 | //字体颜色
40 | private int textColor;
41 | //字体大小
42 | private int textSize;
43 | //每个单独验证码的背景
44 | private Drawable textDrawable;
45 | //验证码选中时的背景
46 | private Drawable textFocusedDrawable;
47 | //是否展示密码样式
48 | private boolean showPassword = false;
49 | //验证码之间间隔
50 | private float dividerWidth = 0;
51 | //对EditText输入进行监听
52 | private TextWatcher watcher;
53 | //监听删除键和enter键
54 | private OnKeyListener onKeyListener;
55 |
56 | //当前选中的TextView位置,即光标所在位置
57 | private int currentFocusPosition = 0;
58 |
59 | public VerificationCodeView(Context context) {
60 | this(context, null);
61 | }
62 |
63 | public VerificationCodeView(Context context, AttributeSet attrs) {
64 | this(context, attrs, 0);
65 | }
66 |
67 | public VerificationCodeView(Context context, AttributeSet attrs, int defStyleAttr) {
68 | super(context, attrs, defStyleAttr);
69 | init(context, attrs, defStyleAttr);
70 | }
71 |
72 | /**
73 | * 初始化View
74 | *
75 | * @param context
76 | * @param attrs
77 | * @param defStyleAttr
78 | */
79 | private void init(Context context, AttributeSet attrs, int defStyleAttr) {
80 | TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.VerificationCodeView, defStyleAttr, 0);
81 | codeNum = array.getInteger(R.styleable.VerificationCodeView_vcv_code_number, 4);
82 | codeWidth = array.getDimensionPixelSize(R.styleable.VerificationCodeView_vcv_code_width, (int) dip2px(50, context));
83 | textColor = array.getColor(R.styleable.VerificationCodeView_vcv_code_color, getResources().getColor(R.color.text_border_focused));
84 | textSize = array.getDimensionPixelSize(R.styleable.VerificationCodeView_vcv_code_size, getResources().getDimensionPixelOffset(R.dimen.text_size));
85 | textDrawable = array.getDrawable(R.styleable.VerificationCodeView_vcv_code_bg_normal);
86 | textFocusedDrawable = array.getDrawable(R.styleable.VerificationCodeView_vcv_code_bg_focus);
87 | showPassword = array.getBoolean(R.styleable.VerificationCodeView_vcv_code_input_style, false);
88 | array.recycle();
89 | //若未设置选中和未选中状态,设置默认
90 | if (textDrawable == null) {
91 | textDrawable = getResources().getDrawable(R.drawable.bg_text_normal);
92 | }
93 | if (textFocusedDrawable == null) {
94 | textFocusedDrawable = getResources().getDrawable(R.drawable.bg_text_focused);
95 | }
96 |
97 | initView(context);
98 | initListener();
99 | resetCursorPosition();
100 | }
101 |
102 | /**
103 | * 初始化各View并加入当前View中
104 | */
105 | private void initView(Context context) {
106 | //初始化TextView数组
107 | textViews = new TextView[codeNum];
108 | for (int i = 0; i < codeNum; i++) {
109 | //循环加入数组中,设置TextView字体大小和颜色,并将TextView依次加入LinearLayout
110 | TextView textView = new TextView(context);
111 | textView.setGravity(Gravity.CENTER);
112 | textView.setTextColor(textColor);
113 | textView.setTextSize(textSize);
114 | textView.setIncludeFontPadding(false);
115 | if(showPassword) {
116 | //数字密码样式,可更改
117 | textView.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
118 | }
119 | textViews[i] = textView;
120 | this.addView(textView);
121 | RelativeLayout.LayoutParams params = (LayoutParams) textView.getLayoutParams();
122 | params.addRule(CENTER_VERTICAL);
123 | params.width = (int) codeWidth;
124 | params.height = (int) codeWidth;
125 | }
126 | //初始化EditText,设置背景色为透明,获取焦点,设置光标颜色,设置输入类型等
127 | editText = new WiseEditText(context);
128 | editText.setBackgroundColor(Color.TRANSPARENT);
129 | editText.requestFocus();
130 | editText.setInputType(InputType.TYPE_CLASS_NUMBER);
131 | setCursorRes(R.drawable.cursor);
132 | addView(editText, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
133 | }
134 |
135 | /**
136 | * 监听EditText输入字符,监听键盘删除键
137 | */
138 | private void initListener() {
139 | watcher = new TextWatcher() {
140 | @Override
141 | public void beforeTextChanged(CharSequence s, int start, int count, int after) {
142 |
143 | }
144 |
145 | @Override
146 | public void onTextChanged(CharSequence s, int start, int before, int count) {
147 |
148 | }
149 |
150 | @Override
151 | public void afterTextChanged(Editable s) {
152 | String content = s.toString();
153 | if (!TextUtils.isEmpty(content)) {
154 | if (content.length() == 1) {
155 | setText(content);
156 | }
157 | editText.setText("");
158 | }
159 | }
160 | };
161 | editText.addTextChangedListener(watcher);
162 |
163 | onKeyListener = new OnKeyListener() {
164 | @Override
165 | public boolean onKey(View v, int keyCode, KeyEvent event) {
166 | if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
167 | deleteCode();
168 | return true;
169 | }
170 | return false;
171 | }
172 | };
173 | editText.setSoftKeyListener(onKeyListener);
174 | }
175 |
176 | /**
177 | * 重设EditText的光标位置,以及选中TextView的边框颜色
178 | */
179 | private void resetCursorPosition() {
180 | for (int i = 0; i < codeNum; i++) {
181 | TextView textView = textViews[i];
182 | if (i == currentFocusPosition) {
183 | textView.setBackgroundDrawable(textFocusedDrawable);
184 | } else {
185 | textView.setBackgroundDrawable(textDrawable);
186 | }
187 | }
188 | if (codeNum > 1) {
189 | if (currentFocusPosition < codeNum) {
190 | //字数小于总数,设置EditText的leftPadding,造成光标移动的错觉
191 | editText.setCursorVisible(true);
192 | float leftPadding = codeWidth / 2 + currentFocusPosition * codeWidth + currentFocusPosition * dividerWidth;
193 | editText.setPadding((int) leftPadding, 0, 0, 0);
194 | } else {
195 | //字数大于总数,隐藏光标
196 | editText.setCursorVisible(false);
197 | }
198 | }
199 | }
200 |
201 | /**
202 | * 删除键按下
203 | */
204 | private void deleteCode() {
205 | if (currentFocusPosition == 0) {
206 | //当前光标位置在0,直接返回
207 | return;
208 | } else {
209 | //当前光标不为0,当前光标位置-1,将当前光标位置TextView置为"",重设光标位置
210 | currentFocusPosition--;
211 | textViews[currentFocusPosition].setText("");
212 | resetCursorPosition();
213 | }
214 | }
215 |
216 | /**
217 | * onMeasure后获取到测量的控件宽度,计算出每个Code之间的间隔
218 | */
219 | private void layoutTextView() {
220 | //获取控件剩余宽度
221 | float availableWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
222 | if (codeNum > 1) {
223 | //计算每个Code之间的间距
224 | dividerWidth = (availableWidth - codeWidth * codeNum) / (codeNum - 1);
225 | for (int i = 1; i < codeNum; i++) {
226 | float leftMargin = codeWidth * i + dividerWidth * i;
227 | RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) textViews[i].getLayoutParams();
228 | params1.leftMargin = (int) leftMargin;
229 | }
230 | }
231 |
232 | //设置EditText宽度从第一个Code左侧到最后一个Code右侧,设置高度为Code高度
233 | //设置EditText为纵向居中
234 | editText.setWidth((int) availableWidth);
235 | editText.setHeight((int) codeWidth);
236 | RelativeLayout.LayoutParams params = (LayoutParams) editText.getLayoutParams();
237 | params.addRule(CENTER_VERTICAL);
238 | }
239 |
240 | /**
241 | * 拦截到EditText输入字符,发送给该方法进行处理
242 | *
243 | * @param s
244 | */
245 | private void setText(String s) {
246 | if (currentFocusPosition >= codeNum) {
247 | //光标已经隐藏,直接返回
248 | return;
249 | }
250 | //设置字符给当前光标位置的TextView,光标位置后移,重设光标状态
251 | textViews[currentFocusPosition].setText(s);
252 | currentFocusPosition++;
253 | resetCursorPosition();
254 | }
255 |
256 | @Override
257 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
258 | int heightMode = MeasureSpec.getMode(heightMeasureSpec);
259 | if (heightMode == MeasureSpec.AT_MOST) {
260 | //若高度是wrap_content,则设置为50dp
261 | heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) dip2px(80, getContext()), MeasureSpec.EXACTLY);
262 | }
263 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
264 | }
265 |
266 | @Override
267 | protected void onLayout(boolean changed, int l, int t, int r, int b) {
268 | super.onLayout(changed, l, t, r, b);
269 | //获取到宽高,可以对TextView进行摆放
270 | if(dividerWidth == 0) {
271 | layoutTextView();
272 | }
273 | }
274 |
275 | private float dip2px(float dp, Context context) {
276 | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
277 | dp, context.getResources().getDisplayMetrics());
278 | }
279 |
280 | /**
281 | * 暴露公共方法,设置光标颜色
282 | *
283 | * @param drawableRes
284 | */
285 | public void setCursorRes(@DrawableRes int drawableRes) {
286 | try {
287 | java.lang.reflect.Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
288 | f.setAccessible(true);
289 | f.set(editText, drawableRes);
290 | } catch (Exception e) {
291 | }
292 | }
293 |
294 | /**
295 | * 获取输入的验证码
296 | *
297 | * @return
298 | */
299 | public String getContent() {
300 | StringBuilder builder = new StringBuilder();
301 | for (TextView tv : textViews) {
302 | builder.append(tv.getText());
303 | }
304 | return builder.toString();
305 | }
306 |
307 | /**
308 | * 判断是否验证码输入完毕
309 | *
310 | * @return
311 | */
312 | public boolean isFinish() {
313 | for (TextView tv : textViews) {
314 | if (TextUtils.isEmpty(tv.getText())) {
315 | return false;
316 | }
317 | }
318 | return true;
319 | }
320 |
321 | /**
322 | * 清除已输入验证码
323 | */
324 | public void clear() {
325 | for (TextView tv : textViews) {
326 | tv.setText("");
327 | }
328 | currentFocusPosition = 0;
329 | resetCursorPosition();
330 | }
331 | }
332 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xylitolz/androidverificationcode/view/WiseEditText.java:
--------------------------------------------------------------------------------
1 | package com.xylitolz.androidverificationcode.view;
2 |
3 | import android.content.Context;
4 | import android.support.v7.widget.AppCompatEditText;
5 | import android.util.AttributeSet;
6 | import android.view.KeyEvent;
7 | import android.view.inputmethod.EditorInfo;
8 | import android.view.inputmethod.InputConnection;
9 | import android.view.inputmethod.InputConnectionWrapper;
10 |
11 | /**
12 | * @author 小米Xylitol
13 | * @email xiaomi987@hotmail.com
14 | * @desc
15 | * @date 2018-05-09 14:56
16 | */
17 | public class WiseEditText extends AppCompatEditText {
18 |
19 |
20 | private OnKeyListener keyListener;
21 |
22 | public WiseEditText(Context context, AttributeSet attrs, int defStyle) {
23 | super(context, attrs, defStyle);
24 | }
25 |
26 | public WiseEditText(Context context, AttributeSet attrs) {
27 | super(context, attrs);
28 | }
29 |
30 | public WiseEditText(Context context) {
31 | super(context);
32 | }
33 |
34 | @Override
35 | public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
36 | return new MyInputConnection(super.onCreateInputConnection(outAttrs),
37 | true);
38 | }
39 |
40 | private class MyInputConnection extends InputConnectionWrapper {
41 |
42 | public MyInputConnection(InputConnection target, boolean mutable) {
43 | super(target, mutable);
44 | }
45 |
46 | @Override
47 | public boolean sendKeyEvent(KeyEvent event) {
48 | if (keyListener != null) {
49 | keyListener.onKey(WiseEditText.this,event.getKeyCode(),event);
50 | }
51 | return super.sendKeyEvent(event);
52 | }
53 |
54 | @Override
55 | public boolean deleteSurroundingText(int beforeLength, int afterLength) {
56 | // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace
57 | if (beforeLength == 1 && afterLength == 0) {
58 | // backspace
59 | return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
60 | && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
61 | }
62 |
63 | return super.deleteSurroundingText(beforeLength, afterLength);
64 | }
65 |
66 | }
67 |
68 | //设置监听回调
69 | public void setSoftKeyListener(OnKeyListener listener){
70 | keyListener = listener;
71 | }
72 |
73 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/bg_text_focused.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/bg_text_normal.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/cursor.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
11 |
16 |
21 |
26 |
31 |
36 |
41 |
46 |
51 |
56 |
61 |
66 |
71 |
76 |
81 |
86 |
91 |
96 |
101 |
106 |
111 |
116 |
121 |
126 |
131 |
136 |
141 |
146 |
151 |
156 |
161 |
166 |
171 |
172 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
26 |
32 |
38 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/riceeater/AndroidVerificationCode/c05876dfe5a484a68f90accd72cdb00bb49db928/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/riceeater/AndroidVerificationCode/c05876dfe5a484a68f90accd72cdb00bb49db928/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/riceeater/AndroidVerificationCode/c05876dfe5a484a68f90accd72cdb00bb49db928/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/riceeater/AndroidVerificationCode/c05876dfe5a484a68f90accd72cdb00bb49db928/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/riceeater/AndroidVerificationCode/c05876dfe5a484a68f90accd72cdb00bb49db928/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/riceeater/AndroidVerificationCode/c05876dfe5a484a68f90accd72cdb00bb49db928/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/riceeater/AndroidVerificationCode/c05876dfe5a484a68f90accd72cdb00bb49db928/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/riceeater/AndroidVerificationCode/c05876dfe5a484a68f90accd72cdb00bb49db928/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/riceeater/AndroidVerificationCode/c05876dfe5a484a68f90accd72cdb00bb49db928/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/riceeater/AndroidVerificationCode/c05876dfe5a484a68f90accd72cdb00bb49db928/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 | #FDB890
7 | #E5E5E5
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 14sp
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AndroidVerificationCode
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/xylitolz/androidverificationcode/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.xylitolz.androidverificationcode;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 |
5 | repositories {
6 | google()
7 | jcenter()
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:3.1.1'
11 |
12 |
13 | // NOTE: Do not place your application dependencies here; they belong
14 | // in the individual module build.gradle files
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | google()
21 | jcenter()
22 | }
23 | }
24 |
25 | task clean(type: Delete) {
26 | delete rootProject.buildDir
27 | }
28 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/riceeater/AndroidVerificationCode/c05876dfe5a484a68f90accd72cdb00bb49db928/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed May 09 10:23:53 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/image/自定义View-验证码仿滴滴.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/riceeater/AndroidVerificationCode/c05876dfe5a484a68f90accd72cdb00bb49db928/image/自定义View-验证码仿滴滴.gif
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------