├── .gitignore ├── .idea ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── GIF.gif ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── nanchen │ │ └── pinviewdemo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── nanchen │ │ │ └── pinviewdemo │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── example_drawable_with_grey_disabled.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 │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── nanchen │ └── pinviewdemo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── nanchen │ │ └── pinview │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── nanchen │ │ │ └── pinview │ │ │ └── PinView.java │ └── res │ │ ├── drawable │ │ └── sample_background.xml │ │ └── values │ │ ├── attrs.xml │ │ ├── color.xml │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── nanchen │ └── pinview │ └── ExampleUnitTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | 34 | 35 | 36 | 37 | 1.8 38 | 39 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /GIF.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nanchen2251/PinView/648f6ed8c28fd1095effc3f26f6185cc11f87e9f/GIF.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PinView 2 | PIN 码专用输入控件,支持任意长度和输入任意数据 3 | 4 | #### 号外:听说「nanchen」搞了一个 Android 开发者的免费福利,不行你看:[给 Android 开发者的一点福利:免费模拟面试](http://mp.weixin.qq.com/s/WRdSmGxwDp-CpQcSlmJYlg) 5 | 6 | - 该项目主要参考了 [https://github.com/GoodieBag/Pinview](https://github.com/GoodieBag/Pinview) 的部分代码。 7 | 8 | 9 | ## 效果图
10 | ![](https://github.com/nanchen2251/PinView/blob/master/GIF.gif) 11 | 12 | #### ⊙开源不易,希望给个star或者fork奖励 13 | #### ⊙拥抱开源:https://github.com/nanchen2251/ 14 | #### ⊙交流群(拒绝无脑问):118116509 Android神技侧漏交流群( 点击图标即可加入 )
15 | 16 | ## 特点 17 | - 支持更改 PinView 中每一个 EditText 的输入前、输入后、正在输入的 background 18 | - 支持设置任意长度的 PIN 码长度 19 | - 支持设置可输入的 PIN 码格式,目前支持文本、数字、可以自己自主设置 20 | - 支持设置输入错误次数限制时间 21 | - 支持设置输入的 PIN 码是否以明文显示 22 | - 支持输入默认值和清除数据 23 | - 支持设置可输入长度和每一个 EditText 的宽高和背景 24 | 25 | 26 | ## 使用方法 27 | #### 1、添加依赖
28 | ##### Step 1. Add it in your root build.gradle at the end of repositories: 29 | ```java 30 | allprojects { 31 | repositories { 32 | ... 33 | maven { url 'https://jitpack.io' } 34 | } 35 | } 36 | ``` 37 | ##### Step 2. Add the dependency 38 | ```java 39 | dependencies { 40 | compile 'com.github.nanchen2251:PinView:1.0.1' 41 | 42 | } 43 | ``` 44 | #### 2、使用方式最好参照 demo
45 | ```java 46 | mPinView.setCanInput(true);// 设置是否可输入 默认true 47 | mPinView.setHint(""); // 设置EditText的Hint 48 | mPinView.setInputType(PinView.InputType.NUMBER); // 设置输入的格式 49 | mPinView.setPassword(true); // 设置是否以明文显示 50 | mPinView.setPinHeight(60); // 设置每一个EditText的高度 51 | mPinView.setPinWidth(60); // 设置每一个EditText的宽度 52 | mPinView.setPinLength(4); // 设置可输入的 PIN 码长度,默认4 53 | mPinView.clearValue(); // 清除输入的数据 54 | mPinView.setValue("1234"); // 设置输入值 55 | mPinView.setPinViewEventListener(this); // 设置输入完毕的监听事件 56 | ``` 57 | #### 3、在 Xml 中使用
58 | ```java 59 | 75 | ``` 76 | ### 关于作者 77 |    南尘
78 |    四川成都
79 |    [其它开源](https://github.com/nanchen2251/)
80 |    [个人博客](https://nanchen2251.github.io/)
81 |    [简书](http://www.jianshu.com/u/f690947ed5a6)
82 |    [博客园](http://www.cnblogs.com/liushilin/)
83 |    交流群:118116509
84 |    欢迎投稿(关注)我的唯一公众号,公众号搜索 nanchen 或者扫描下方二维码:
85 |    ![](http://images2015.cnblogs.com/blog/845964/201707/845964-20170718083641599-1963842541.jpg) 86 | 87 | 88 | > 1024 - 梦想,永不止步! 89 | 爱编程 不爱Bug 90 | 爱加班 不爱黑眼圈 91 | 固执 但不偏执 92 | 疯狂 但不疯癫 93 | 生活里的菜鸟 94 | 工作中的大神 95 | 身怀宝藏,一心憧憬星辰大海 96 | 追求极致,目标始于高山之巅 97 | 一群怀揣好奇,梦想改变世界的孩子 98 | 一群追日逐浪,正在改变世界的极客 99 | 你们用最美的语言,诠释着科技的力量 100 | 你们用极速的创新,引领着时代的变迁 101 | 102 | ------至所有正在努力奋斗的程序猿们!加油!! 103 | 104 | ## Licenses 105 | ``` 106 | Copyright 2018 nanchen(刘世麟) 107 | 108 | Licensed under the Apache License, Version 2.0 (the "License"); 109 | you may not use this file except in compliance with the License. 110 | You may obtain a copy of the License at 111 | 112 | http://www.apache.org/licenses/LICENSE-2.0 113 | 114 | Unless required by applicable law or agreed to in writing, software 115 | distributed under the License is distributed on an "AS IS" BASIS, 116 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 117 | See the License for the specific language governing permissions and 118 | limitations under the License. 119 | ``` 120 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | defaultConfig { 6 | applicationId "com.nanchen.pinviewdemo" 7 | minSdkVersion 15 8 | targetSdkVersion 26 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(include: ['*.jar'], dir: 'libs') 23 | implementation 'com.android.support:appcompat-v7:26.0.0-beta1' 24 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:0.5' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2' 28 | implementation project(':library') 29 | } 30 | -------------------------------------------------------------------------------- /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/nanchen/pinviewdemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.nanchen.pinviewdemo; 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() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.nanchen.pinviewdemo", 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/nanchen/pinviewdemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.nanchen.pinviewdemo; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.os.Bundle; 5 | import android.os.CountDownTimer; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.widget.TextView; 8 | import android.widget.Toast; 9 | 10 | import com.nanchen.pinview.PinView; 11 | import com.nanchen.pinview.PinView.PinViewEventListener; 12 | 13 | public class MainActivity extends AppCompatActivity implements PinViewEventListener { 14 | 15 | private TextView mTvTime; 16 | private PinView mPinView; 17 | 18 | @Override 19 | protected void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_main); 22 | 23 | mTvTime = findViewById(R.id.tv_time_cut); 24 | mPinView = findViewById(R.id.pinView); 25 | 26 | // mPinView.setCanInput(true);// 设置是否可输入 默认true 27 | // mPinView.setHint(""); // 设置EditText的Hint 28 | // mPinView.setInputType(PinView.InputType.NUMBER); // 设置输入的格式 29 | // mPinView.setPassword(true); // 设置是否以明文显示 30 | // mPinView.setPinHeight(60); // 设置每一个EditText的高度 31 | // mPinView.setPinWidth(60); // 设置每一个EditText的宽度 32 | // mPinView.setPinLength(4); // 设置可输入的 PIN 码长度,默认4 33 | // mPinView.clearValue(); // 清除输入的数据 34 | // mPinView.setValue("1234"); // 设置输入值 35 | mPinView.setPinViewEventListener(this); // 设置输入完毕的监听事件 36 | } 37 | 38 | int count = 0; 39 | 40 | @Override 41 | public void onDataEntered(PinView pinview, boolean fromUser) { 42 | Toast.makeText(this, pinview.getValue(), Toast.LENGTH_SHORT).show(); 43 | // String value = pinview.getValue(); 44 | // 45 | // String trueValue = getSharedPreferences(GlobalConfig.PREFERENCES_KEY, MODE_PRIVATE).getString(GlobalConfig.PIN_KEY, null); 46 | // if (TextUtils.equals(trueValue, value)) { 47 | // // 如果输入正确,进入到主页 48 | // startActivity(new Intent(this, HomeActivity.class)); 49 | // finish(); 50 | // } else { 51 | count++; 52 | mPinView.setValue(""); 53 | // 清空 54 | mTvTime.setText("输入错误,还可尝试" + (5 - count) + "次"); 55 | if (count >= 5) { // 大于5次需要等待10秒 56 | long millisInFuture = 10 * 1000; 57 | if (mTimeCount != null) 58 | mTimeCount.cancel(); 59 | mTimeCount = new MyTimeCount(millisInFuture, 1000); 60 | mTimeCount.start(); 61 | mPinView.setCanInput(false); 62 | } 63 | // } 64 | } 65 | 66 | MyTimeCount mTimeCount; 67 | 68 | 69 | private class MyTimeCount extends CountDownTimer { 70 | 71 | /** 72 | * @param millisInFuture The number of millis in the future from the call 73 | * to {@link #start()} until the countdown is done and {@link #onFinish()} 74 | * is called. 75 | * @param countDownInterval The interval along the way to receive 76 | * {@link #onTick(long)} callbacks. 77 | */ 78 | public MyTimeCount(long millisInFuture, long countDownInterval) { 79 | super(millisInFuture, countDownInterval); 80 | } 81 | 82 | @SuppressLint("SetTextI18n") 83 | @Override 84 | public void onTick(long millisUntilFinished) { 85 | mTvTime.setText("输入错误," + (millisUntilFinished / 1000) + "秒后再试"); 86 | } 87 | 88 | @Override 89 | public void onFinish() { 90 | mTvTime.setText("输入密码"); 91 | mPinView.setCanInput(true); 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /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/example_drawable_with_grey_disabled.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /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 | 10 | 11 | 20 | 21 | 22 | 28 | 29 | 34 | 35 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /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/nanchen2251/PinView/648f6ed8c28fd1095effc3f26f6185cc11f87e9f/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nanchen2251/PinView/648f6ed8c28fd1095effc3f26f6185cc11f87e9f/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nanchen2251/PinView/648f6ed8c28fd1095effc3f26f6185cc11f87e9f/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nanchen2251/PinView/648f6ed8c28fd1095effc3f26f6185cc11f87e9f/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nanchen2251/PinView/648f6ed8c28fd1095effc3f26f6185cc11f87e9f/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nanchen2251/PinView/648f6ed8c28fd1095effc3f26f6185cc11f87e9f/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nanchen2251/PinView/648f6ed8c28fd1095effc3f26f6185cc11f87e9f/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nanchen2251/PinView/648f6ed8c28fd1095effc3f26f6185cc11f87e9f/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nanchen2251/PinView/648f6ed8c28fd1095effc3f26f6185cc11f87e9f/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nanchen2251/PinView/648f6ed8c28fd1095effc3f26f6185cc11f87e9f/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #fff 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | PinViewDemo 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/nanchen/pinviewdemo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.nanchen.pinviewdemo; 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() throws Exception { 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.0.0' 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 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nanchen2251/PinView/648f6ed8c28fd1095effc3f26f6185cc11f87e9f/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Jan 04 10:58:41 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.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 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 Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 26 5 | 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 15 10 | targetSdkVersion 26 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(dir: 'libs', include: ['*.jar']) 29 | 30 | implementation 'com.android.support:appcompat-v7:26.0.0-beta1' 31 | testImplementation 'junit:junit:4.12' 32 | androidTestImplementation 'com.android.support.test:runner:0.5' 33 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2' 34 | } 35 | -------------------------------------------------------------------------------- /library/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 | -------------------------------------------------------------------------------- /library/src/androidTest/java/com/nanchen/pinview/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.nanchen.pinview; 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() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.nanchen.pinview.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /library/src/main/java/com/nanchen/pinview/PinView.java: -------------------------------------------------------------------------------- 1 | package com.nanchen.pinview; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Rect; 6 | import android.support.annotation.DrawableRes; 7 | import android.support.annotation.NonNull; 8 | import android.text.Editable; 9 | import android.text.InputFilter; 10 | import android.text.TextWatcher; 11 | import android.text.method.NumberKeyListener; 12 | import android.text.method.TransformationMethod; 13 | import android.util.AttributeSet; 14 | import android.view.Gravity; 15 | import android.view.KeyEvent; 16 | import android.view.MotionEvent; 17 | import android.view.View; 18 | import android.view.inputmethod.InputMethodManager; 19 | import android.widget.EditText; 20 | import android.widget.LinearLayout; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | import static android.text.InputType.TYPE_CLASS_NUMBER; 26 | import static android.text.InputType.TYPE_CLASS_TEXT; 27 | import static android.text.InputType.TYPE_NUMBER_VARIATION_PASSWORD; 28 | 29 | /** 30 | * GitHub 地址:https://github.com/nanchen2251/PinView 31 | * 32 | * 33 | * Author: nanchen 34 | * Email: liushilin520@foxmail.com 35 | * Date: 2018-01-04 11:03 36 | */ 37 | 38 | public class PinView extends LinearLayout implements TextWatcher, View.OnFocusChangeListener, View.OnKeyListener { 39 | private final float DENSITY = getContext().getResources().getDisplayMetrics().density; 40 | private int mPinLength = 4; // 默认的Pin长度 41 | private List editTextList = new ArrayList<>(); 42 | private int mPinWidth = 50; 43 | private int mPinHeight = 50; 44 | private int mSplitWidth = 20; 45 | private boolean mCursorVisible = false; 46 | private boolean mDelPressed = false; 47 | @DrawableRes 48 | private int mPinBackground = R.drawable.sample_background; 49 | private boolean mPassword = false; 50 | private String mHint = ""; 51 | private InputType inputType = InputType.TEXT; 52 | private boolean finalNumberPin = false; 53 | private PinViewEventListener mListener; 54 | private boolean fromSetValue = false; 55 | private boolean mForceKeyboard = true; 56 | 57 | public enum InputType { 58 | TEXT, NUMBER 59 | } 60 | 61 | /** 62 | * Interface for onDataEntered event. 63 | */ 64 | 65 | public interface PinViewEventListener { 66 | void onDataEntered(PinView pinview, boolean fromUser); 67 | } 68 | 69 | OnClickListener mClickListener; 70 | 71 | View currentFocus = null; 72 | 73 | InputFilter filters[] = new InputFilter[1]; 74 | LinearLayout.LayoutParams params; 75 | 76 | 77 | public PinView(Context context) { 78 | this(context, null); 79 | } 80 | 81 | public PinView(Context context, AttributeSet attrs) { 82 | this(context, attrs, 0); 83 | } 84 | 85 | public PinView(Context context, AttributeSet attrs, int defStyleAttr) { 86 | super(context, attrs, defStyleAttr); 87 | setGravity(Gravity.CENTER); 88 | init(context, attrs, defStyleAttr); 89 | } 90 | 91 | /** 92 | * A method to take care of all the initialisations. 93 | * 94 | * @param context 95 | * @param attrs 96 | * @param defStyleAttr 97 | */ 98 | private void init(Context context, AttributeSet attrs, int defStyleAttr) { 99 | this.removeAllViews(); 100 | mPinHeight *= DENSITY; 101 | mPinWidth *= DENSITY; 102 | mSplitWidth *= DENSITY; 103 | setWillNotDraw(false); 104 | initAttributes(context, attrs, defStyleAttr); 105 | params = new LayoutParams(mPinWidth, mPinHeight); 106 | setOrientation(HORIZONTAL); 107 | createEditTexts(); 108 | super.setOnClickListener(new OnClickListener() { 109 | @Override 110 | public void onClick(View view) { 111 | boolean focused = false; 112 | for (EditText editText : editTextList) { 113 | if (editText.length() == 0) { 114 | editText.requestFocus(); 115 | openKeyboard(); 116 | focused = true; 117 | break; 118 | } 119 | } 120 | if (!focused && editTextList.size() > 0) { // Focus the last view 121 | editTextList.get(editTextList.size() - 1).requestFocus(); 122 | } 123 | if (mClickListener != null) { 124 | mClickListener.onClick(PinView.this); 125 | } 126 | } 127 | }); 128 | // Bring up the keyboard 129 | final View firstEditText = editTextList.get(0); 130 | if (firstEditText != null) 131 | firstEditText.postDelayed(new Runnable() { 132 | @Override 133 | public void run() { 134 | openKeyboard(); 135 | } 136 | }, 200); 137 | updateEnabledState(); 138 | } 139 | 140 | /** 141 | * Creates editTexts and adds it to the pinview based on the pinLength specified. 142 | */ 143 | 144 | private void createEditTexts() { 145 | removeAllViews(); 146 | editTextList.clear(); 147 | EditText editText; 148 | for (int i = 0; i < mPinLength; i++) { 149 | editText = new EditText(getContext()); 150 | editTextList.add(i, editText); 151 | this.addView(editText); 152 | generateOneEditText(editText, "" + i); 153 | } 154 | setTransformation(); 155 | } 156 | 157 | @Override 158 | public void setEnabled(boolean enabled) { 159 | for (EditText editText : editTextList) { 160 | editText.setEnabled(enabled); 161 | } 162 | } 163 | 164 | @Override 165 | public void setFocusable(boolean focusable) { 166 | super.setFocusable(focusable); 167 | for (EditText editText : editTextList) { 168 | editText.setEnabled(focusable); 169 | } 170 | } 171 | 172 | @Override 173 | public void setFocusableInTouchMode(boolean focusableInTouchMode) { 174 | super.setFocusableInTouchMode(focusableInTouchMode); 175 | for (EditText editText : editTextList) { 176 | editText.setEnabled(focusableInTouchMode); 177 | } 178 | } 179 | 180 | public void setCanInput(boolean canInput) { 181 | setEnabled(canInput); 182 | setFocusable(canInput); 183 | if (!canInput) { 184 | for (EditText editText : editTextList) { 185 | editText.setKeyListener(null); 186 | } 187 | } else { 188 | for (EditText editText : editTextList) { 189 | editText.setKeyListener(new NumberKeyListener() { 190 | @NonNull 191 | @Override 192 | protected char[] getAcceptedChars() { 193 | return new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; 194 | } 195 | 196 | @Override 197 | public int getInputType() { 198 | return android.text.InputType.TYPE_CLASS_NUMBER; 199 | } 200 | }); 201 | } 202 | } 203 | } 204 | 205 | 206 | /** 207 | * This method gets the attribute values from the XML, if not found it takes the default values. 208 | * 209 | * @param context 210 | * @param attrs 211 | * @param defStyleAttr 212 | */ 213 | private void initAttributes(Context context, AttributeSet attrs, int defStyleAttr) { 214 | if (attrs != null) { 215 | final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.PinView, defStyleAttr, 0); 216 | mPinBackground = array.getResourceId(R.styleable.PinView_pinBackground, mPinBackground); 217 | mPinLength = array.getInt(R.styleable.PinView_pinLength, mPinLength); 218 | mPinHeight = (int) array.getDimension(R.styleable.PinView_pinHeight, mPinHeight); 219 | mPinWidth = (int) array.getDimension(R.styleable.PinView_pinWidth, mPinWidth); 220 | mSplitWidth = (int) array.getDimension(R.styleable.PinView_splitWidth, mSplitWidth); 221 | mCursorVisible = array.getBoolean(R.styleable.PinView_cursorVisible, mCursorVisible); 222 | mPassword = array.getBoolean(R.styleable.PinView_password, mPassword); 223 | mForceKeyboard = array.getBoolean(R.styleable.PinView_forceKeyboard, mForceKeyboard); 224 | mHint = array.getString(R.styleable.PinView_hint); 225 | InputType[] its = InputType.values(); 226 | inputType = its[array.getInt(R.styleable.PinView_inputType, 0)]; 227 | array.recycle(); 228 | } 229 | } 230 | 231 | /** 232 | * Takes care of styling the editText passed in the param. 233 | * tag is the index of the editText. 234 | * 235 | * @param styleEditText 236 | * @param tag 237 | */ 238 | private void generateOneEditText(EditText styleEditText, String tag) { 239 | params.setMargins(mSplitWidth / 2, mSplitWidth / 2, mSplitWidth / 2, mSplitWidth / 2); 240 | filters[0] = new InputFilter.LengthFilter(1); 241 | styleEditText.setFilters(filters); 242 | styleEditText.setLayoutParams(params); 243 | styleEditText.setGravity(Gravity.CENTER); 244 | styleEditText.setCursorVisible(mCursorVisible); 245 | styleEditText.setTextColor(getResources().getColor(R.color.pin_white)); 246 | 247 | if (!mCursorVisible) { 248 | styleEditText.setClickable(false); 249 | styleEditText.setHint(mHint); 250 | 251 | styleEditText.setOnTouchListener(new OnTouchListener() { 252 | @Override 253 | public boolean onTouch(View view, MotionEvent motionEvent) { 254 | // When back space is pressed it goes to delete mode and when u click on an edit Text it should get out of the delete mode 255 | mDelPressed = false; 256 | return false; 257 | } 258 | }); 259 | } 260 | styleEditText.setBackgroundResource(mPinBackground); 261 | styleEditText.setPadding(0, 0, 0, 0); 262 | styleEditText.setTag(tag); 263 | styleEditText.setInputType(getKeyboardInputType()); 264 | styleEditText.addTextChangedListener(this); 265 | styleEditText.setOnFocusChangeListener(this); 266 | styleEditText.setOnKeyListener(this); 267 | } 268 | 269 | private int getKeyboardInputType() { 270 | int it; 271 | switch (inputType) { 272 | case NUMBER: 273 | it = TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_PASSWORD; 274 | break; 275 | case TEXT: 276 | it = TYPE_CLASS_TEXT; 277 | break; 278 | default: 279 | it = TYPE_CLASS_TEXT; 280 | } 281 | return it; 282 | } 283 | 284 | /** 285 | * Returns the value of the PinView 286 | * 287 | * @return 288 | */ 289 | 290 | public String getValue() { 291 | StringBuilder sb = new StringBuilder(); 292 | for (EditText et : editTextList) { 293 | sb.append(et.getText().toString()); 294 | } 295 | return sb.toString(); 296 | } 297 | 298 | /** 299 | * Requsets focus on current pin view and opens keyboard if forceKeyboard is enabled. 300 | * 301 | * @return the current focused pin view. It can be used to open softkeyboard manually. 302 | */ 303 | public View requestPinEntryFocus() { 304 | int currentTag = Math.max(0, getIndexOfCurrentFocus()); 305 | EditText currentEditText = editTextList.get(currentTag); 306 | if (currentEditText != null) { 307 | currentEditText.requestFocus(); 308 | } 309 | openKeyboard(); 310 | return currentEditText; 311 | } 312 | 313 | public void openKeyboard() { 314 | if (mForceKeyboard) { 315 | InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 316 | inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); 317 | } 318 | } 319 | 320 | /** 321 | * Clears the values in the PinView 322 | */ 323 | public void clearValue() { 324 | setValue(""); 325 | } 326 | 327 | /** 328 | * Sets the value of the PinView 329 | * 330 | * @param value 331 | */ 332 | public void setValue(@NonNull String value) { 333 | String regex = "[0-9]*"; // Allow empty string to clear the fields 334 | fromSetValue = true; 335 | if (inputType == InputType.NUMBER && !value.matches(regex)) 336 | return; 337 | int lastTagHavingValue = -1; 338 | for (int i = 0; i < editTextList.size(); i++) { 339 | if (value.length() > i) { 340 | lastTagHavingValue = i; 341 | editTextList.get(i).setText(((Character) value.charAt(i)).toString()); 342 | } else { 343 | editTextList.get(i).setText(""); 344 | } 345 | } 346 | if (mPinLength > 0) { 347 | if (lastTagHavingValue < mPinLength - 1) { 348 | currentFocus = editTextList.get(lastTagHavingValue + 1); 349 | } else { 350 | currentFocus = editTextList.get(mPinLength - 1); 351 | if (inputType == InputType.NUMBER || mPassword) 352 | finalNumberPin = true; 353 | if (mListener != null) 354 | mListener.onDataEntered(this, false); 355 | } 356 | currentFocus.requestFocus(); 357 | } 358 | fromSetValue = false; 359 | updateEnabledState(); 360 | } 361 | 362 | @Override 363 | public void onFocusChange(View view, boolean isFocused) { 364 | if (isFocused && !mCursorVisible) { 365 | if (mDelPressed) { 366 | currentFocus = view; 367 | mDelPressed = false; 368 | return; 369 | } 370 | for (final EditText editText : editTextList) { 371 | if (editText.length() == 0) { 372 | if (editText != view) { 373 | editText.requestFocus(); 374 | } else { 375 | currentFocus = view; 376 | } 377 | return; 378 | } 379 | } 380 | if (editTextList.get(editTextList.size() - 1) != view) { 381 | editTextList.get(editTextList.size() - 1).requestFocus(); 382 | } else { 383 | currentFocus = view; 384 | } 385 | } else if (isFocused && mCursorVisible) { 386 | currentFocus = view; 387 | } else { 388 | view.clearFocus(); 389 | } 390 | } 391 | 392 | /** 393 | * Handles the character transformation for password inputs. 394 | */ 395 | private void setTransformation() { 396 | if (mPassword) { 397 | for (EditText editText : editTextList) { 398 | editText.removeTextChangedListener(this); 399 | editText.setTransformationMethod(new PinTransformationMethod()); 400 | editText.addTextChangedListener(this); 401 | } 402 | } else { 403 | for (EditText editText : editTextList) { 404 | editText.removeTextChangedListener(this); 405 | editText.setTransformationMethod(null); 406 | editText.addTextChangedListener(this); 407 | } 408 | } 409 | } 410 | 411 | @Override 412 | public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 413 | 414 | } 415 | 416 | /** 417 | * Fired when text changes in the editTexts. 418 | * Backspace is also identified here. 419 | * 420 | * @param charSequence 421 | * @param start 422 | * @param i1 423 | * @param count 424 | */ 425 | @Override 426 | public void onTextChanged(CharSequence charSequence, int start, int i1, int count) { 427 | if (charSequence.length() == 1 && currentFocus != null) { 428 | final int currentTag = getIndexOfCurrentFocus(); 429 | if (currentTag < mPinLength - 1) { 430 | long delay = 1; 431 | if (mPassword) 432 | delay = 25; 433 | this.postDelayed(new Runnable() { 434 | @Override 435 | public void run() { 436 | EditText nextEditText = editTextList.get(currentTag + 1); 437 | nextEditText.setEnabled(true); 438 | nextEditText.requestFocus(); 439 | } 440 | }, delay); 441 | } else { 442 | //Last Pin box has been reached. 443 | } 444 | if (currentTag == mPinLength - 1 && inputType == InputType.NUMBER || currentTag == mPinLength - 1 && mPassword) { 445 | finalNumberPin = true; 446 | } 447 | 448 | } else if (charSequence.length() == 0) { 449 | int currentTag = getIndexOfCurrentFocus(); 450 | mDelPressed = true; 451 | //For the last cell of the non password text fields. Clear the text without changing the focus. 452 | if (editTextList.get(currentTag).getText().length() > 0) 453 | editTextList.get(currentTag).setText(""); 454 | } 455 | 456 | for (int index = 0; index < mPinLength; index++) { 457 | if (editTextList.get(index).getText().length() < 1) 458 | break; 459 | if (!fromSetValue && index + 1 == mPinLength && mListener != null) 460 | mListener.onDataEntered(this, true); 461 | } 462 | updateEnabledState(); 463 | } 464 | 465 | /** 466 | * Disable views ahead of current focus, so a selector can change the drawing of those views. 467 | */ 468 | private void updateEnabledState() { 469 | int currentTag = Math.max(0, getIndexOfCurrentFocus()); 470 | for (int index = 0; index < editTextList.size(); index++) { 471 | EditText editText = editTextList.get(index); 472 | editText.setEnabled(index <= currentTag); 473 | } 474 | } 475 | 476 | @Override 477 | public void afterTextChanged(Editable editable) { 478 | 479 | } 480 | 481 | /** 482 | * Monitors keyEvent. 483 | * 484 | * @param view 485 | * @param i 486 | * @param keyEvent 487 | * @return 488 | */ 489 | @Override 490 | public boolean onKey(View view, int i, KeyEvent keyEvent) { 491 | 492 | if ((keyEvent.getAction() == KeyEvent.ACTION_UP) && (i == KeyEvent.KEYCODE_DEL)) { 493 | // Perform action on Del press 494 | int currentTag = getIndexOfCurrentFocus(); 495 | //Last tile of the number pad. Clear the edit text without changing the focus. 496 | if (inputType == InputType.NUMBER && currentTag == mPinLength - 1 && finalNumberPin || 497 | (mPassword && currentTag == mPinLength - 1 && finalNumberPin)) { 498 | if (editTextList.get(currentTag).length() > 0) { 499 | editTextList.get(currentTag).setText(""); 500 | } 501 | finalNumberPin = false; 502 | } else if (currentTag > 0) { 503 | mDelPressed = true; 504 | if (editTextList.get(currentTag).length() == 0) { 505 | //Takes it back one tile 506 | editTextList.get(currentTag - 1).requestFocus(); 507 | //Clears the tile it just got to 508 | editTextList.get(currentTag).setText(""); 509 | } else { 510 | //If it has some content clear it first 511 | editTextList.get(currentTag).setText(""); 512 | } 513 | } else { 514 | //For the first cell 515 | if (editTextList.get(currentTag).getText().length() > 0) 516 | editTextList.get(currentTag).setText(""); 517 | } 518 | return true; 519 | 520 | } 521 | 522 | return false; 523 | } 524 | 525 | /** 526 | * A class to implement the transformation mechanism 527 | */ 528 | private class PinTransformationMethod implements TransformationMethod { 529 | 530 | private char BULLET = '\u2022'; 531 | 532 | @Override 533 | public CharSequence getTransformation(CharSequence source, final View view) { 534 | return new PasswordCharSequence(source); 535 | } 536 | 537 | @Override 538 | public void onFocusChanged(final View view, final CharSequence sourceText, final boolean focused, final int direction, final Rect previouslyFocusedRect) { 539 | 540 | } 541 | 542 | private class PasswordCharSequence implements CharSequence { 543 | private final CharSequence source; 544 | 545 | public PasswordCharSequence(@NonNull CharSequence source) { 546 | this.source = source; 547 | } 548 | 549 | @Override 550 | public int length() { 551 | return source.length(); 552 | } 553 | 554 | @Override 555 | public char charAt(int index) { 556 | return BULLET; 557 | } 558 | 559 | @Override 560 | public CharSequence subSequence(int start, int end) { 561 | return new PasswordCharSequence(this.source.subSequence(start, end)); 562 | } 563 | 564 | } 565 | } 566 | 567 | /** 568 | * Getters and Setters 569 | */ 570 | private int getIndexOfCurrentFocus() { 571 | return editTextList.indexOf(currentFocus); 572 | } 573 | 574 | 575 | public int getSplitWidth() { 576 | return mSplitWidth; 577 | } 578 | 579 | public void setSplitWidth(int splitWidth) { 580 | this.mSplitWidth = splitWidth; 581 | int margin = splitWidth / 2; 582 | params.setMargins(margin, margin, margin, margin); 583 | 584 | for (EditText editText : editTextList) { 585 | editText.setLayoutParams(params); 586 | } 587 | } 588 | 589 | public int getPinHeight() { 590 | return mPinHeight; 591 | } 592 | 593 | public void setPinHeight(int pinHeight) { 594 | this.mPinHeight = pinHeight; 595 | params.height = pinHeight; 596 | for (EditText editText : editTextList) { 597 | editText.setLayoutParams(params); 598 | } 599 | } 600 | 601 | public int getPinWidth() { 602 | return mPinWidth; 603 | } 604 | 605 | public void setPinWidth(int pinWidth) { 606 | this.mPinWidth = pinWidth; 607 | params.width = pinWidth; 608 | for (EditText editText : editTextList) { 609 | editText.setLayoutParams(params); 610 | } 611 | } 612 | 613 | public int getPinLength() { 614 | return mPinLength; 615 | } 616 | 617 | public void setPinLength(int pinLength) { 618 | this.mPinLength = pinLength; 619 | createEditTexts(); 620 | } 621 | 622 | public boolean isPassword() { 623 | return mPassword; 624 | } 625 | 626 | public void setPassword(boolean password) { 627 | this.mPassword = password; 628 | setTransformation(); 629 | } 630 | 631 | public String getHint() { 632 | return mHint; 633 | } 634 | 635 | public void setHint(String mHint) { 636 | this.mHint = mHint; 637 | for (EditText editText : editTextList) 638 | editText.setHint(mHint); 639 | } 640 | 641 | public 642 | @DrawableRes 643 | int getPinBackground() { 644 | return mPinBackground; 645 | } 646 | 647 | public void setPinBackgroundRes(@DrawableRes int res) { 648 | this.mPinBackground = res; 649 | for (EditText editText : editTextList) 650 | editText.setBackgroundResource(res); 651 | } 652 | 653 | @Override 654 | public void setOnClickListener(OnClickListener l) { 655 | mClickListener = l; 656 | } 657 | 658 | public InputType getInputType() { 659 | return inputType; 660 | } 661 | 662 | public void setInputType(InputType inputType) { 663 | this.inputType = inputType; 664 | int it = getKeyboardInputType(); 665 | for (EditText editText : editTextList) { 666 | editText.setInputType(it); 667 | } 668 | } 669 | 670 | public void setPinViewEventListener(PinViewEventListener listener) { 671 | this.mListener = listener; 672 | } 673 | } 674 | -------------------------------------------------------------------------------- /library/src/main/res/drawable/sample_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 9 | -------------------------------------------------------------------------------- /library/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 | -------------------------------------------------------------------------------- /library/src/main/res/values/color.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFF 4 | -------------------------------------------------------------------------------- /library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | PinView 3 | 4 | -------------------------------------------------------------------------------- /library/src/test/java/com/nanchen/pinview/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.nanchen.pinview; 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() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':library' 2 | --------------------------------------------------------------------------------