Android自定义验证码输入框
Android开发的验证码输入框,输入够自动触发下一步
一、思路:
自定义验证码控件,监听每一个输入框
二、效果图:
看视频更直观点:
Android开发轮子-自定义验证码输入框
三、关键代码:
public class PhoneCode extends RelativeLayout {
private Context context;
private TextView tv_code1;
private TextView tv_code2;
private TextView tv_code3;
private TextView tv_code4;
// private TextView tv_code5;
// private TextView tv_code6;
private EditText et_code;
private List<String> codes = new ArrayList<>();
private InputMethodManager imm;
public PhoneCode(Context context) {
super(context);
this.context = context;
loadView();
}
public PhoneCode(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
loadView();
}
private void loadView() {
imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
View view = LayoutInflater.from(context).inflate(R.layout.phone_code, this);
initView(view);
initEvent();
}
private void initView(View view) {
tv_code1 = (TextView) view.findViewById(R.id.tv_code1);
tv_code2 = (TextView) view.findViewById(R.id.tv_code2);
tv_code3 = (TextView) view.findViewById(R.id.tv_code3);
tv_code4 = (TextView) view.findViewById(R.id.tv_code4);
// tv_code5 = (TextView) view.findViewById(R.id.tv_code5);
// tv_code6 = (TextView) view.findViewById(R.id.tv_code6);
et_code = (EditText) view.findViewById(R.id.et_code);
et_code.requestFocus();
// et_code.setLongClickable(true);//支持长按
}
public EditText getEt_code() {
return et_code;
}
public void clearCode() {
tv_code1.setText("");
tv_code2.setText("");
tv_code3.setText("");
tv_code4.setText("");
// tv_code5.setText("");
// tv_code6.setText("");
codes.clear();
setTvDrawable();
}
public void etGetFocus(){
et_code.requestFocus();
}
private void initEvent() {
//验证码输入
et_code.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (editable != null && editable.length() > 0) {
et_code.setText("");
if (codes.size() < 4) {
String data = editable.toString().trim();
if (data.length() >= 4) {
//将string转换成List
List<String> list = Arrays.asList(data.split(""));
//Arrays.asList没有实现add和remove方法,继承的AbstractList,需要将list放进java.util.ArrayList中
codes = new ArrayList<>(list);
if (!codes.isEmpty() && codes.size() > 4) {
//使用data.split("")方法会将""放进第一下标里需要去掉
codes.remove(0);
}
} else {
codes.add(data);
}
showCode();
}
}
}
});
// 监听验证码删除按键
et_code.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if (!codes.isEmpty()) {
if (keyCode == KeyEvent.KEYCODE_DEL && keyEvent.getAction() == KeyEvent.ACTION_DOWN && codes.size() > 0) {
codes.remove(codes.size() - 1);
showCode();
return true;
}
}
return false;
}
});
}
/**
* 显示输入的验证码
*/
private void showCode() {
String code1 = "";
String code2 = "";
String code3 = "";
String code4 = "";
String code5 = "";
String code6 = "";
if (codes.size() >= 1) {
code1 = codes.get(0);
}
if (codes.size() >= 2) {
code2 = codes.get(1);
}
if (codes.size() >= 3) {
code3 = codes.get(2);
}
if (codes.size() >= 4) {
code4 = codes.get(3);
}
if (codes.size() >= 5) {
code5 = codes.get(4);
}
if (codes.size() >= 6) {
code6 = codes.get(5);
}
setTvDrawable();
tv_code1.setText(code1);
tv_code2.setText(code2);
tv_code3.setText(code3);
tv_code4.setText(code4);
// tv_code5.setText(code5);
// tv_code6.setText(code6);
//setColor();//设置高亮颜色
callBack();//回调
}
四、项目demo源码结构图:
有问题或者需要完整demo源码的私信我,我每天都看私信的