Android自定义控件:验证码输入框

需求: 输入一个4位验证码输入框:
Android自定义控件:验证码输入框_第1张图片
具体实现:
1、布局文件 phone_code.xml



    
    
        
            
        
        
        
            
        
        
        
            
        
        
        
            
        
        
        
            
        
        
        
            
        
    

    


2、自定义控件 PhoneCode

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 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);//支持长按
    }

    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() < 6) {
                        String data = editable.toString().trim();
                        if (data.length() >= 6) {
                            //将string转换成List
                            List list = Arrays.asList(data.split(""));
                            //Arrays.asList没有实现add和remove方法,继承的AbstractList,需要将list放进java.util.ArrayList中
                            codes = new ArrayList<>(list);
                            if (EmptyUtils.isNotEmpty(codes) && codes.size() > 6) {
                                //使用data.split("")方法会将""放进第一下标里需要去掉
                                codes.remove(0);
                            }
                        } else {
                            codes.add(data);
                        }
                        showCode();
                    }
                }
            }
        });
        // 监听验证码删除按键
        et_code.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                if (EmptyUtils.isNotEmpty(codes)) {
                    if (keyCode == KeyEvent.KEYCODE_DEL && keyEvent.getAction() == KeyEvent.ACTION_DOWN && codes.size() > 0) {
                        codes.remove(codes.size() - 1);
                        showCode();
                        return true;
                    }
                }
                return false;
            }
        });

        final XPopup.Builder builder = new XPopup.Builder(getContext()).watchView(et_code);
        et_code.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                builder.asAttachList(new String[]{"粘贴"}, null, new OnSelectListener() {
                    @Override
                    public void onSelect(int selectPostion, String text) {
                        //剪贴板中允许四种类型,Html, String,Uri,Intent,不能直接获取ClipData.getItemAt(0).getText()这样仅是获取剪贴板中第一项是文本的,如果第一项不是文本,获取null
                        String data = ClipboardUtils.getTextFromText();//获取文本
                        if(EmptyUtils.isEmpty(data)){
                            return;
                        }
                        if (data.length() >= 6) {
                            //将string转换成List
                            List list = Arrays.asList(data.split(""));
                            //Arrays.asList没有实现add和remove方法,继承的AbstractList,需要将list放进java.util.ArrayList中
                            codes = new ArrayList<>(list);
                            if (EmptyUtils.isNotEmpty(codes) && codes.size() > 6) {
                                //使用data.split("")方法会将“”放进第一下标里需要去掉
                                codes.remove(0);
                            }
                        } else {
                            codes.add(data);
                        }
                        showCode();
                    }
                }).show();
                return true;
            }
        });
    }

    /**
     * 显示输入的验证码
     */
    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);
        }
        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();//回调
    }

    /**
     * 显示输入的验证码
     */
    public void showEmptyCode() {
        tv_code1.setText("");
        tv_code2.setText("");
        tv_code3.setText("");
        tv_code4.setText("");
        tv_code5.setText("");
        tv_code6.setText("");
        codes.clear();
        et_code.setText("");
    }

    /**
     * 设置高亮颜色
     */
    /*private void setColor(){
        int color_default = Color.parseColor("#999999");
        int color_focus = Color.parseColor("#3F8EED");
        v1.setBackgroundColor(color_default);
        v2.setBackgroundColor(color_default);
        v3.setBackgroundColor(color_default);
        v4.setBackgroundColor(color_default);
        if(codes.size()==0){
            v1.setBackgroundColor(color_focus);
        }
        if(codes.size()==1){
            v2.setBackgroundColor(color_focus);
        }
        if(codes.size()==2){
            v3.setBackgroundColor(color_focus);
        }
        if(codes.size()>=3){
            v4.setBackgroundColor(color_focus);
        }
    }*/

    /**
     * 回调
     */
    private void callBack() {
        if (onInputListener == null) {
            return;
        }
        if (codes.size() == 6) {
            onInputListener.onSucess(getPhoneCode());
        } else {
            onInputListener.onInput();
        }
    }

    //定义回调
    public interface OnInputListener {
        void onSucess(String codes);

        void onInput();
    }
    private OnInputListener onInputListener;
    public void setOnInputListener(OnInputListener onInputListener) {
        this.onInputListener = onInputListener;
    }

    /**
     * 显示键盘
     */
    public void showSoftInput() {
        //显示软键盘
        if (imm != null && et_code != null) {
            et_code.postDelayed(new Runnable() {
                @Override
                public void run() {
                    imm.showSoftInput(et_code, 0);
                }
            }, 1500);
        }
    }

    /**
     * 获得手机号验证码
     *
     * @return 验证码
     */
    public String getPhoneCode() {
        StringBuilder sb = new StringBuilder();
        for (String codes : codes) {
            sb.append(codes);
        }
        return sb.toString();
    }
}

3、剪贴板相关工具类ClipboardUtils

    /**
     * 获取剪贴板的文本,仅获取Item中类型是String的文本
     *
     * @return 剪贴板的文本
     */
    public static String getTextFromText() {
        ClipboardManager clipboard = (ClipboardManager) AppUtils.getAppContext().getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = clipboard.getPrimaryClip();
        if (clip != null && clip.getItemCount() > 0 && EmptyUtils.isNotEmpty(clip.getItemAt(0).getText())) {
            return clip.getItemAt(0).getText().toString();
        }
        return null;
    }

你可能感兴趣的:(Android自定义控件:验证码输入框)