盗个别人的效果图 我的这个控件实现了可以动态配置4位 或者6位 多位的验证码 支持在layout里面动态配置
public class Verificationcode extends RelativeLayout {
private Context context;
private TextView tv_code;
private View lineView;
private EditText et_code;
private List<String> codes = new ArrayList<>();
private int textColor;
private int codeNumber;
private float textSize;
private LinearLayout ll_content;
private int lineColorDefault;
private int lineColorFocus;
public Verificationcode(Context context) {
this(context, null);
}
public Verificationcode(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public Verificationcode(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
loadView(attrs);
}
private void loadView(AttributeSet attrs) {
View view = LayoutInflater.from(context).inflate(R.layout.verification_code, this);
ll_content = view.findViewById(R.id.ll_code_content);
et_code = view.findViewById(R.id.et_code);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Verificationcode);
textColor = typedArray.getColor(R.styleable.Verificationcode_code_text_color, getResources().getColor(R.color.white));
lineColorDefault = typedArray.getColor(R.styleable.Verificationcode_line_color_default, getResources().getColor(R.color.white));
lineColorFocus = typedArray.getColor(R.styleable.Verificationcode_line_color_focus, getResources().getColor(R.color.color_3F8EED));
codeNumber = typedArray.getInt(R.styleable.Verificationcode_code_number, 6);
textSize = typedArray.getDimension(R.styleable.Verificationcode_code_text_size, getResources().getDimension(R.dimen.text_20));
initEvent();
initView();
}
private void initView() {
for (int i = 0; i < codeNumber; i++) {
LinearLayout.LayoutParams layout_param = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
View item_view = LayoutInflater.from(context).inflate(R.layout.verifation_code_item, null);
tv_code = item_view.findViewById(R.id.tv_code);
tv_code.setTextColor(textColor);
tv_code.setTextSize(textSize);
lineView = item_view.findViewById(R.id.view);
lineView.setBackgroundColor(lineColorDefault);
ll_content.addView(item_view, layout_param);
}
et_code.setFocusable(true);
et_code.setFocusableInTouchMode(true);
et_code.requestFocus();
showKeyboard(et_code);
}
private void initEvent() {
et_code.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable editable) {
if (editable != null && editable.length() > 0) {
et_code.setText("");
if (codes.size() < codeNumber) {
codes.add(editable.toString());
showCode(editable.toString());
}
}
}
});
// 监听验证码删除按键
et_code.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_DEL && keyEvent.getAction() == KeyEvent.ACTION_DOWN && codes.size() > 0) {
if (codes.isEmpty()) return false;
removeCode(codes.size() - 1);
codes.remove(codes.size() - 1);
return true;
}
return false;
}
});
}
/**
* 显示输入的验证码
*/
private void showCode(String code) {
LinearLayout child = (LinearLayout) ll_content.getChildAt(codes.size() - 1);
TextView tvCode = (TextView) child.getChildAt(0);
View lineView = child.getChildAt(1);
tvCode.setText(code);
lineView.setBackgroundColor(lineColorFocus);
callBack();//回调
}
private void removeCode(int code) {
LinearLayout child = (LinearLayout) ll_content.getChildAt(code);
TextView tvCode = (TextView) child.getChildAt(0);
View lineView = child.getChildAt(1);
tvCode.setText("");
lineView.setBackgroundColor(lineColorDefault);
}
/**
* 回调
*/
private void callBack() {
if (onInputListener == null) {
return;
}
if (codes.size() == codeNumber) {
onInputListener.onSucess(getPhoneCode());
} else {
onInputListener.onInput();
}
}
//定义回调
public interface OnInputListener {
void onSucess(String code);
void onInput();
}
private OnInputListener onInputListener;
public void setOnInputListener(OnInputListener onInputListener) {
this.onInputListener = onInputListener;
}
/**
* 获得手机号验证码
*
* @return 验证码
*/
public String getPhoneCode() {
StringBuilder sb = new StringBuilder();
for (String code : codes) {
sb.append(code);
}
return sb.toString();
}
private void showKeyboard(EditText editText){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(editText, InputMethodManager.RESULT_SHOWN);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
verifation_code_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="7dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/tv_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:gravity="center"
android:textColor="@color/white"
android:textSize="20sp" />
<View
android:id="@+id/view"
android:layout_width="40dp"
android:layout_height="1dp" />
</LinearLayout>
verification_code.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/ll_code_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<EditText
android:id="@+id/et_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/ll_code_content"
android:layout_alignBottom="@+id/ll_code_content"
android:background="@android:color/transparent"
android:cursorVisible="false"
android:inputType="number"
android:textColor="@android:color/transparent" />
</RelativeLayout>
attrs.xml里面添加自定义属性配置
<declare-styleable name="Verificationcode">
<attr name="code_text_color" format="color" />//验证码字体颜色
<attr name="code_text_size" format="dimension" />//验证码字体大小
<attr name="code_number" format="integer" />//验证码数量 4位 6位
<attr name="line_color_default" format="color" />//验证码下面线的默认颜色
<attr name="line_color_focus" format="color" />//验证码下面线选中后的颜色
</declare-styleable>
使用方法
<cn.onion.commonui.Verificationcode
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:id="@+id/verification_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="83dp" />
设置监听
verificationCode.setOnInputListener(new Verificationcode.OnInputListener() {
@Override
public void onSucess(String code) {
}
@Override
public void onInput() {
}
});