增加inputType
This commit is contained in:
@@ -13,8 +13,8 @@
|
||||
android:orientation="horizontal"
|
||||
app:vcv_et_bg="@drawable/et_login_code"
|
||||
app:vcv_et_cursor="@drawable/et_cursor"
|
||||
app:vcv_et_inputType="text"
|
||||
app:vcv_et_number="6"
|
||||
app:vcv_et_text_color="@android:color/black"
|
||||
app:vcv_et_text_size="15" />
|
||||
|
||||
app:vcv_et_text_size="6sp" />
|
||||
</LinearLayout>
|
||||
|
||||
@@ -37,6 +37,10 @@ public class VerificationCodeView extends LinearLayout implements TextWatcher, V
|
||||
*/
|
||||
private int mEtNumber;
|
||||
|
||||
/**
|
||||
* 输入框类型
|
||||
*/
|
||||
private VCInputType mEtInputType;
|
||||
/**
|
||||
* 输入框的宽度
|
||||
*/
|
||||
@@ -59,12 +63,21 @@ public class VerificationCodeView extends LinearLayout implements TextWatcher, V
|
||||
|
||||
private int mCursorDrawable;
|
||||
|
||||
public enum VCInputType {
|
||||
NUMBER,
|
||||
NUMBERPASSWORD,
|
||||
TEXT,
|
||||
TEXTPASSWORD,
|
||||
}
|
||||
|
||||
public VerificationCodeView(Context context, @Nullable AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
this.mContext = context;
|
||||
@SuppressLint({"Recycle", "CustomViewStyleable"})
|
||||
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.vericationCodeView);
|
||||
mEtNumber = typedArray.getInteger(R.styleable.vericationCodeView_vcv_et_number, 4);
|
||||
int inputType = typedArray.getInt(R.styleable.vericationCodeView_vcv_et_inputType, VCInputType.NUMBER.ordinal());
|
||||
mEtInputType = VCInputType.values()[inputType];
|
||||
mEtWidth = typedArray.getDimensionPixelSize(R.styleable.vericationCodeView_vcv_et_width, 120);
|
||||
mEtTextColor = typedArray.getColor(R.styleable.vericationCodeView_vcv_et_text_color, Color.BLACK);
|
||||
mEtTextSize = typedArray.getDimensionPixelSize(R.styleable.vericationCodeView_vcv_et_text_size, 16);
|
||||
@@ -80,6 +93,15 @@ public class VerificationCodeView extends LinearLayout implements TextWatcher, V
|
||||
private void initView() {
|
||||
for (int i = 0; i < mEtNumber; i++) {
|
||||
EditText editText = new EditText(mContext);
|
||||
initEditText(editText, i);
|
||||
addView(editText);
|
||||
if (i == 0) {
|
||||
editText.setFocusable(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void initEditText(EditText editText, int i) {
|
||||
int childHPadding = 14;
|
||||
int childVPadding = 14;
|
||||
|
||||
@@ -89,7 +111,6 @@ public class VerificationCodeView extends LinearLayout implements TextWatcher, V
|
||||
layoutParams.leftMargin = childHPadding;
|
||||
layoutParams.rightMargin = childHPadding;
|
||||
layoutParams.gravity = Gravity.CENTER;
|
||||
|
||||
editText.setLayoutParams(layoutParams);
|
||||
editText.setGravity(Gravity.CENTER);
|
||||
editText.setId(i);
|
||||
@@ -99,11 +120,28 @@ public class VerificationCodeView extends LinearLayout implements TextWatcher, V
|
||||
editText.setTextSize(mEtTextSize);
|
||||
editText.setMaxLines(1);
|
||||
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1)});
|
||||
switch (mEtInputType) {
|
||||
case NUMBER:
|
||||
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
|
||||
break;
|
||||
case NUMBERPASSWORD:
|
||||
editText.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD);
|
||||
break;
|
||||
case TEXT:
|
||||
editText.setInputType(InputType.TYPE_CLASS_TEXT);
|
||||
break;
|
||||
case TEXTPASSWORD:
|
||||
editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
|
||||
break;
|
||||
default:
|
||||
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
|
||||
}
|
||||
editText.setPadding(0, 0, 0, 0);
|
||||
editText.setOnKeyListener(this);
|
||||
editText.setBackgroundResource(mEtTextBg);
|
||||
try {//修改光标的颜色(反射)
|
||||
|
||||
//修改光标的颜色(反射)
|
||||
try {
|
||||
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
|
||||
f.setAccessible(true);
|
||||
f.set(editText, mCursorDrawable);
|
||||
@@ -112,13 +150,9 @@ public class VerificationCodeView extends LinearLayout implements TextWatcher, V
|
||||
editText.addTextChangedListener(this);
|
||||
editText.setOnFocusChangeListener(this);
|
||||
editText.setOnKeyListener(this);
|
||||
addView(editText);
|
||||
if (i == 0) {
|
||||
editText.setFocusable(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
|
||||
|
||||
@@ -4,6 +4,13 @@
|
||||
<declare-styleable name="vericationCodeView">
|
||||
<!--输入框的数量-->
|
||||
<attr name="vcv_et_number" format="integer" />
|
||||
<!--输入类型-->
|
||||
<attr name="vcv_et_inputType">
|
||||
<enum name="number" value="0" />
|
||||
<enum name="numberPassword" value="1" />
|
||||
<enum name="text" value="2" />
|
||||
<enum name="textPassword" value="3" />
|
||||
</attr>
|
||||
<!--输入框的宽度-->
|
||||
<attr name="vcv_et_width" format="dimension|reference" />
|
||||
<!--输入框文字颜色-->
|
||||
|
||||
Reference in New Issue
Block a user