CaptchaView
CaptchaView是一個驗證碼长踊、邀請碼的自定義布局,是在 InputCodeLayout
的基礎(chǔ)上修改了原生系統(tǒng)鍵盤刪除鍵不起作用的問題、添加了自定義脫敏樣式和自定義digits
過濾及回調(diào)丧枪。
效果預(yù)覽
image.png
如何使用
只需要在 build.gradle
的 dependencies
添加:
compile 'com.opensource:captchaview:1.0.3'
布局中配置
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:label="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<com.captcha.CaptchaView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
label:captchaHeight="56dp"
label:captchaWidth="56dp"
label:inputMode="number"
label:number="4"/>
</LinearLayout>
屬性 | 描述 |
---|---|
label:number | 驗證碼可輸入的個數(shù) |
android:textColor | 字體顏色 |
android:textSize | 字體大小 |
android:gravity | CaptchaView 位置 |
label:captchaWidth | 輸入框的寬度 |
label:captchaHeight | 輸入框的高度 |
label:dividerWidth | 輸入框的間距 |
label:hasFocusBackground | 是否顯示獲取焦點時背景光涂,默認(rèn)顯示 unFocusBackground 背景 |
label:focusBackground | 獲取焦點時背景 |
label:unFocusBackground | 失去焦點時背景 |
label:passwordTransformation | 不可見時樣式,如 ● 拧烦、* 忘闻,不指定為可見顯示 |
label:inputMode | 輸入模式,number 只可輸入數(shù)字恋博,text 任何齐佳,numberText 只可輸入數(shù)字和字母 |
代碼中設(shè)置
CaptchaView captchaView = (CaptchaView) findViewById(R.id.captchaView);
//設(shè)置間距
captchaView.setDivideWidth(20);
//設(shè)置輸入框個數(shù)
captchaView.setNumber(5);
//設(shè)置顯示位置
captchaView.setGravity(Gravity.CENTER);
//輸入類型過濾,如果需指定 digits债沮,這里的InputType必須為CaptchaView.INPUT_TYPE_NUMBER_TEXT才起作用
captchaView.setInputType(CaptchaView.INPUT_TYPE_NUMBER_TEXT, "1234efcsf*");
添加回調(diào)
captchaView.setOnInputCompleteListener(new CaptchaView.OnInputCompleteCallback() {
@Override
public void onInputCompleteListener(String captcha) {
// 輸入完成回調(diào)
}
@Override
public void onError(String error) {
// InputType必須為CaptchaView.INPUT_TYPE_NUMBER_TEXT炼吴,才回調(diào)
// error 為不在 digits 中的字符
}
});
感謝
https://stackoverflow.com/questions/4886858/android-edittext-deletebackspace-key-event
License
Copyright 2017 utouch
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.