自定义验证码输入框

前言:本博客属于自定义组合控件,思想是在textview上放一个透明的edittext 使用stringbuffer追加到textview中

下面开始撸代码:

1.
public class CustomEditTextCode extends RelativeLayout implements TextWatcher, View.OnKeyListener {

    private EditText ed;  
    private TextView[] textViews; //使用数组将textview装起来
    private StringBuffer stringBuffer = new StringBuffer();
    private int count; //长度
    private String inputString;  //内容
    private OnChangeLintener onChangeLintener; //监听

    public CustomEditTextCode(Context context) {
        this(context,null);
    }

    public CustomEditTextCode(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomEditTextCode(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        textViews=new TextView[4];
        View.inflate(context, R.layout.popup_dialog_custom_edcode,this);
        ed = findViewById(R.id.custom_ed);
        textViews[0]=findViewById(R.id.custom_codetvone);
        textViews[1]=findViewById(R.id.custom_codetvtwo);
        textViews[2]=findViewById(R.id.custom_codetvthree);
        textViews[3]=findViewById(R.id.custom_codetvfour);

        ed.setCursorVisible(false);    //隐藏光标

        ChangeLintener();  //EditText监听

    }

    private void ChangeLintener() {
        ed.addTextChangedListener(this);   //输入监听
        ed.setOnKeyListener(this);        //delete 监听
    }

    @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 s) {
         if (!s.toString().equals("")){  //判断输入不为空
             if (stringBuffer.length()>3){
                 ed.setText("");
                 return;
             }else {
                 stringBuffer.append(s);
                 ed.setText("");
                 count = stringBuffer.length();  //长度
                 inputString = stringBuffer.toString();
                 if (stringBuffer.length()==4){  //当长度等于4时,调用完成输入时的监听
                     if (onChangeLintener!=null){
                         onChangeLintener.onInputHigherlimitLintener(inputString.trim());
                     }
                 }
             }
             for (int i= 0;i< stringBuffer.length();i++){
                     textViews[i].setText(String.valueOf(inputString.charAt(i)));
                     textViews[i].setBackgroundResource(R.drawable.custom_edcodebg_yes);
                  }

         }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DEL
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (onKeyDelete()) return true;
            return true;
        }
        return false;
    }

    private boolean onKeyDelete() {
        if (count == 0) {
            count = 4;
            if (onChangeLintener!=null){
                onChangeLintener.onDeleteLintener(false);
            }
            return true;
        }
        if (stringBuffer.length() > 0) {
            //删除相应位置的字符
            stringBuffer.delete((count - 1), count);
            count--;
            textViews[stringBuffer.length()].setText("");
            textViews[stringBuffer.length()].setBackgroundResource(R.drawable.custom_edcodebg_no);  //删除的背景
            if (onChangeLintener!=null){
                onChangeLintener.onDeleteLintener(true);
            }

        }
        return false;
    }

    public void addEditTextChangeLintener(OnChangeLintener onChangeLintener){
        this.onChangeLintener=onChangeLintener;
    }

    public interface OnChangeLintener{

        void onDeleteLintener(boolean delete);

        void onInputHigherlimitLintener(String s);

    }
}

 

2.
R.drawable.custom_edcodebg_yes


    
        
    
    
        
    

 

3.

R.drawable.custom_edcodebg_no

    
        
    
    
        
    

 

4.

布局文件:



      
            
            
            
            
      
      

你可能感兴趣的:(android)