package com.doumee.lib_coremodel.view; 
 | 
  
 | 
import android.content.Context; 
 | 
import android.content.res.TypedArray; 
 | 
import android.text.Editable; 
 | 
import android.text.TextWatcher; 
 | 
import android.util.AttributeSet; 
 | 
import android.view.KeyEvent; 
 | 
import android.view.LayoutInflater; 
 | 
import android.view.View; 
 | 
import android.view.inputmethod.InputMethodManager; 
 | 
import android.widget.EditText; 
 | 
import android.widget.LinearLayout; 
 | 
import android.widget.RelativeLayout; 
 | 
import android.widget.TextView; 
 | 
  
 | 
import com.doumee.lib_coremodel.R; 
 | 
import com.doumee.lib_coremodel.util.DpOrSp2PxUtil; 
 | 
  
 | 
import java.util.ArrayList; 
 | 
import java.util.List; 
 | 
  
 | 
  
 | 
/** 
 | 
 * 类:PhoneCode 
 | 
 * 作者: qxc 
 | 
 * 日期:2018/3/14. 
 | 
 */ 
 | 
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 View v1; 
 | 
    private View v2; 
 | 
    private View v3; 
 | 
    private View v4; 
 | 
    private EditText et_code; 
 | 
    private List<String> codes = new ArrayList<>(); 
 | 
    private InputMethodManager imm; 
 | 
  
 | 
    private int textColorNormal; 
 | 
    private int textColor; 
 | 
    private int lineColorNormal; 
 | 
    private int lineColor; 
 | 
  
 | 
    private int d = 0; 
 | 
  
 | 
    public PhoneCode(Context context) { 
 | 
        super(context); 
 | 
        this.context = context; 
 | 
        loadView(); 
 | 
    } 
 | 
  
 | 
    public PhoneCode(Context context, AttributeSet attrs) { 
 | 
        super(context, attrs); 
 | 
        this.context = context; 
 | 
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.PhoneCode); 
 | 
        textColorNormal = ta.getColor(R.styleable.PhoneCode_textColorNormal,0xffE5E5E5 ); 
 | 
        textColor = ta.getColor(R.styleable.PhoneCode_textColor, 0xff70D0D2); 
 | 
        lineColorNormal = ta.getColor(R.styleable.PhoneCode_lineColorNormal, textColorNormal); 
 | 
        lineColor = ta.getColor(R.styleable.PhoneCode_lineColor, textColor); 
 | 
        d = DpOrSp2PxUtil.dp2pxConvertInt(getContext(),1); 
 | 
        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); 
 | 
        et_code = (EditText) view.findViewById(R.id.et_code); 
 | 
        v1 = view.findViewById(R.id.v1); 
 | 
        v2 = view.findViewById(R.id.v2); 
 | 
        v3 = view.findViewById(R.id.v3); 
 | 
        v4 = view.findViewById(R.id.v4); 
 | 
        tv_code1.setTextColor(textColor); 
 | 
        tv_code2.setTextColor(textColor); 
 | 
        tv_code3.setTextColor(textColor); 
 | 
        tv_code4.setTextColor(textColor); 
 | 
    } 
 | 
  
 | 
    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){ 
 | 
                        codes.add(editable.toString()); 
 | 
                        showCode(); 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        }); 
 | 
        // 监听验证码删除按键 
 | 
        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) { 
 | 
                    codes.remove(codes.size()-1); 
 | 
                    showCode(); 
 | 
                    return true; 
 | 
                } 
 | 
                return false; 
 | 
            } 
 | 
        }); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 显示输入的验证码 
 | 
     */ 
 | 
    private void showCode(){ 
 | 
        String code1 = ""; 
 | 
        String code2 = ""; 
 | 
        String code3 = ""; 
 | 
        String code4 = ""; 
 | 
        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); 
 | 
        } 
 | 
        tv_code1.setText(code1); 
 | 
        tv_code2.setText(code2); 
 | 
        tv_code3.setText(code3); 
 | 
        tv_code4.setText(code4); 
 | 
  
 | 
        setColor();//设置高亮颜色 
 | 
        callBack();//回调 
 | 
    } 
 | 
  
 | 
    public void clearCode(){ 
 | 
        codes.clear(); 
 | 
        showCode(); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 设置高亮颜色 
 | 
     */ 
 | 
    private void setColor(){ 
 | 
        v1.setBackgroundColor(lineColorNormal); 
 | 
        v2.setBackgroundColor(lineColorNormal); 
 | 
        v3.setBackgroundColor(lineColorNormal); 
 | 
        v4.setBackgroundColor(lineColorNormal); 
 | 
        if(codes.size()==1){ 
 | 
            v1.setBackgroundColor(lineColor); 
 | 
        } 
 | 
        if(codes.size()==2){ 
 | 
            v1.setBackgroundColor(lineColor); 
 | 
            v2.setBackgroundColor(lineColor); 
 | 
        } 
 | 
        if(codes.size()==3){ 
 | 
            v1.setBackgroundColor(lineColor); 
 | 
            v2.setBackgroundColor(lineColor); 
 | 
            v3.setBackgroundColor(lineColor); 
 | 
        } 
 | 
        if(codes.size()>=4){ 
 | 
            v1.setBackgroundColor(lineColor); 
 | 
            v2.setBackgroundColor(lineColor); 
 | 
            v3.setBackgroundColor(lineColor); 
 | 
            v4.setBackgroundColor(lineColor); 
 | 
        } 
 | 
        setBlod(v1,codes.size()>0); 
 | 
        setBlod(v2,codes.size()>1); 
 | 
        setBlod(v3,codes.size()>2); 
 | 
        setBlod(v4,codes.size()>3); 
 | 
    } 
 | 
  
 | 
    private void setBlod(View v,boolean isBlod){ 
 | 
        LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) v.getLayoutParams(); 
 | 
        params.height = d * (isBlod?3:1); 
 | 
        v.setLayoutParams(params); 
 | 
    } 
 | 
  
 | 
    public void etRequestFocus(){ 
 | 
        et_code.requestFocus(); 
 | 
        showSoftInput(); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 回调 
 | 
     */ 
 | 
    private void callBack(){ 
 | 
        if(onInputListener==null){ 
 | 
            return; 
 | 
        } 
 | 
        if(codes.size()==4){ 
 | 
            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; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 显示键盘 
 | 
     */ 
 | 
    public void showSoftInput(){ 
 | 
        //显示软键盘 
 | 
        if(imm!=null && et_code!=null) { 
 | 
            et_code.postDelayed(new Runnable() { 
 | 
                @Override 
 | 
                public void run() { 
 | 
                    imm.showSoftInput(et_code, 0); 
 | 
                } 
 | 
            },200); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 获得手机号验证码 
 | 
     * @return 验证码 
 | 
     */ 
 | 
    public String getPhoneCode(){ 
 | 
        StringBuilder sb = new StringBuilder(); 
 | 
        for (String code : codes) { 
 | 
            sb.append(code); 
 | 
        } 
 | 
        return sb.toString(); 
 | 
    } 
 | 
} 
 |