密码输入框
v1.5
- 增加了PasswordDialog,采用Builder模式,便于使用
- 精简了代码和自定义属性,只保留主要功能,以降低代码大小和使用成本
- 获得焦点时可改变颜色,区分获得焦点的控件和未获得焦点的控件
- 具有过渡动画,在输入、删除、焦点切换时进行较好的过渡,解决用户使用时的突兀感
- 使字符方块固定为正方形,取消了长方形的情况
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}dependencies {
compile 'com.github.EthanCo:PasswordInput:1.5.5'
}在你的项目中加入PasswordInput Library,之后在xml使用即可
<com.ethanco.lib.PasswordInput
android:id="@+id/passwordInput_first"android:layout_width="match_parent"android:layout_height="56dp"app:boxCount="6"app:focusedColor="@color/colorAccent"app:notFocusedColor="@color/colorPrimary" />PasswordInputpasswordInput = (PasswordInput) findViewById(R.id.passwordInput);
StringpwdFirst = passwordInput.getText().toString();passwordInput.setTextLenChangeListen(newPasswordInput.TextLenChangeListen() {
@OverridepublicvoidonTextLenChange(CharSequencetext, intlen) {
//do something
}
});passwordInput.setPassword("123456")
passwordInput.setPassword("")
passwordInput.dismiss()<!--获得焦点时的颜色-->
<attrname="focusedColor"format="color" />
<!--未获得焦点时的颜色-->
<attrname="notFocusedColor"format="color" />
<!--背景色-->
<attrname="backgroundColor"format="color" />
<!--字符方块的数量-->
<attrname="boxCount"format="integer" />
<!--获得焦点时颜色是否改变-->
<attrname="focusColorChangeEnable"format="boolean" />
<!--圆点半径-->
<attrname="dotRaduis"format="dimension" /> PasswordDialog.Builderbuilder = newPasswordDialog.Builder(MainActivity.this)
.setTitle(R.string.please_input_password) //Dialog标题//.setBoxCount(4) //设置密码位数,此方法有BUG,建议在Builder构造函数中传入layoutRes来定义
.setBorderNotFocusedColor(R.color.colorSecondaryText) //边框颜色
.setDotNotFocusedColor(R.color.colorSecondaryText) //密码圆点颜色
.setPositiveListener(newOnPositiveButtonListener() { @Override//确定publicvoidonPositiveClick(DialogInterfacedialog, intwhich, Stringtext) {
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
})
.setNegativeListener(newDialogInterface.OnClickListener() {
@Override//取消publicvoidonClick(DialogInterfacedialogInterface, inti) {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
}
})
.addCheckPasswordFilter(newCountCheckFilter()); //添加过滤器builder.create().show();可进行过滤器的添加,对于不符合输入要求的,返回为false,将不会执行NegativeListener回调。
publicclassCountCheckFilterimplementsICheckPasswordFilter {
@OverridepublicbooleanonCheckPassword(Contextcontext, CharSequencepassword) {
if (password.length() != 4) {
Toast.makeText(context, "密码需为4位", Toast.LENGTH_SHORT).show();
returnfalse;
}
returntrue;
}
}默认已添加密码为空情况的过滤器
builder.setPositiveText("确定");builder.setNegativeText("取消");builder.setBoxRadius(3); //单位为dpbuidler.setBoxMarge(3); //单位为dpbuidler.setBorderNotFocusedColor(R.color.colorSecondaryText)
.setDotNotFocusedColor(R.color.colorSecondaryText);如需更改PositiveButton和NevegateButton的颜色,将
<colorname="colorAccent">#FF4081</color>复制到app的color.xml中修改即可。
builder..setCancelable(false);将PasswordInput的tag设为passwordInput_dialog
<?xml version="1.0" encoding="utf-8"?>
<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="wrap_content"android:layout_height="wrap_content">
<com.ethanco.lib.PasswordInput
android:id="@+id/pwdInput_dialog"android:layout_width="@dimen/dialog_password_input_width"android:layout_height="@dimen/dialog_password_input_height"android:layout_gravity="center"android:layout_margin="8dp"android:cursorVisible="false"android:focusable="true"android:inputType="number"android:maxLength="4"android:tag="passwordInput_dialog"app:focusColorChangeEnable="false" />
</FrameLayout>PasswordDialog.Builderbuilder = newPasswordDialog.Builder(MainActivity.this, R.layout.custom_dialog_password);


