|
@@ -48,6 +48,8 @@ public class VerifyEditText extends LinearLayout {
|
|
|
private Context mContext;
|
|
|
//输入完成监听
|
|
|
private InputCompleteListener mInputCompleteListener;
|
|
|
+ private boolean isPassword;
|
|
|
+ private int passwordVisibleTime;
|
|
|
|
|
|
public VerifyEditText(Context context) {
|
|
|
this(context, null);
|
|
@@ -74,18 +76,18 @@ public class VerifyEditText extends LinearLayout {
|
|
|
ContextCompat.getColor(context, android.R.color.black));
|
|
|
int count = obtainStyledAttributes.getInt(R.styleable.verify_EditText_verify_count, DEFAULT_ITEM_COUNT);
|
|
|
int inputType = obtainStyledAttributes.getInt(R.styleable.verify_EditText_verify_inputType, InputType.TYPE_CLASS_NUMBER);
|
|
|
- int passwordVisibleTime = obtainStyledAttributes.getInt(R.styleable.verify_EditText_verify_password_visible_time, DEFAULT_PASSWORD_VISIBLE_TIME);
|
|
|
+ passwordVisibleTime = obtainStyledAttributes.getInt(R.styleable.verify_EditText_verify_password_visible_time, DEFAULT_PASSWORD_VISIBLE_TIME);
|
|
|
int width = (int) obtainStyledAttributes.getDimension(R.styleable.verify_EditText_verify_width, DEFAULT_ITEM_WIDTH);
|
|
|
int height = (int) obtainStyledAttributes.getDimension(R.styleable.verify_EditText_verify_height, 0);
|
|
|
int margin = (int) obtainStyledAttributes.getDimension(R.styleable.verify_EditText_verify_margin, DEFAULT_ITEM_MARGIN);
|
|
|
- float textSize = px2sp(context,obtainStyledAttributes.getDimension(R.styleable.verify_EditText_verify_textSize, sp2px(context,DEFAULT_ITEM_TEXT_SIZE)));
|
|
|
- boolean password = obtainStyledAttributes.getBoolean(R.styleable.verify_EditText_verify_password, false);
|
|
|
+ float textSize = px2sp(context, obtainStyledAttributes.getDimension(R.styleable.verify_EditText_verify_textSize, sp2px(context, DEFAULT_ITEM_TEXT_SIZE)));
|
|
|
+ isPassword = obtainStyledAttributes.getBoolean(R.styleable.verify_EditText_verify_password, false);
|
|
|
obtainStyledAttributes.recycle();
|
|
|
if (count < 2) count = 2;//最少 2 个 item
|
|
|
|
|
|
mEditText = new EditText(context);
|
|
|
mEditText.setInputType(inputType);
|
|
|
- mEditText.setLayoutParams(new LinearLayout.LayoutParams(1, 1));
|
|
|
+ mEditText.setLayoutParams(new LayoutParams(1, 1));
|
|
|
mEditText.setCursorVisible(false);
|
|
|
mEditText.setBackground(null);
|
|
|
mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(count)});//限制输入长度为 count
|
|
@@ -98,17 +100,19 @@ public class VerifyEditText extends LinearLayout {
|
|
|
@Override
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
|
TextView textView = mTextViewList.get(start);//获取对应的 textview
|
|
|
- if (before == 0) {//输入
|
|
|
- CharSequence input = s.subSequence(start, s.length());//获取新输入的字
|
|
|
- textView.setText(input);
|
|
|
- if (password) {//如果需要密文显示
|
|
|
- textView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
|
|
|
- //passwordVisibleTime 毫秒后设置为密文显示
|
|
|
- textView.postDelayed(() ->
|
|
|
- textView.setTransformationMethod(PasswordTransformationMethod.getInstance()),
|
|
|
- passwordVisibleTime);
|
|
|
- }
|
|
|
- setTextViewBackground(textView, drawableSelected);
|
|
|
+ if (before == 0 || count > 1) {//输入
|
|
|
+ updateText(String.valueOf(s));
|
|
|
+ mEditText.setSelection(s.length());
|
|
|
+// CharSequence input = s.subSequence(start, s.length());//获取新输入的字
|
|
|
+// textView.setText(input);
|
|
|
+// if (isPassword) {//如果需要密文显示
|
|
|
+// textView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
|
|
|
+// //passwordVisibleTime 毫秒后设置为密文显示
|
|
|
+// textView.postDelayed(() ->
|
|
|
+// textView.setTransformationMethod(PasswordTransformationMethod.getInstance()),
|
|
|
+// passwordVisibleTime);
|
|
|
+// }
|
|
|
+// setTextViewBackground(textView, drawableSelected);
|
|
|
} else {//删除
|
|
|
textView.setText("");
|
|
|
setTextViewBackground(textView, drawableNormal);
|
|
@@ -146,6 +150,26 @@ public class VerifyEditText extends LinearLayout {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void updateText(String content) {
|
|
|
+ char[] chars = content.toCharArray();
|
|
|
+ int min = Math.min(chars.length, mTextViewList.size());
|
|
|
+ for (int i = 0; i < min; i++) {
|
|
|
+ char aChar = chars[i];
|
|
|
+ String s = String.valueOf(aChar);
|
|
|
+ TextView textView = mTextViewList.get(i);
|
|
|
+ textView.setText(s);
|
|
|
+ if (isPassword) {//如果需要密文显示
|
|
|
+ textView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
|
|
|
+ //passwordVisibleTime 毫秒后设置为密文显示
|
|
|
+ textView.postDelayed(() ->
|
|
|
+ textView.setTransformationMethod(PasswordTransformationMethod.getInstance()),
|
|
|
+ passwordVisibleTime);
|
|
|
+ }
|
|
|
+ setTextViewBackground(textView, drawableSelected);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* view 添加到窗口时,延迟 500ms 弹出软键盘
|
|
|
*/
|
|
@@ -157,6 +181,7 @@ public class VerifyEditText extends LinearLayout {
|
|
|
|
|
|
/**
|
|
|
* 设置背景
|
|
|
+ *
|
|
|
* @param textView
|
|
|
* @param drawable
|
|
|
*/
|