六个格子的密码输入框

遇到一个小需求 app需要做一个类似微信密码支付的六个小格子的输入框,网上查了一下,还是有蛮多的解决方案,看了一下他们的思路,然后大致自己写了一个
思路 写连续六个输入框,根据输入框的获取焦点的情况和输入字数的情况,判断是否使下一个输入框获取焦点,根据监听删除按钮判断是否使上一个输入框获取焦点

代码详解
1 包装Edittext代码

public class LastInputEditText extends EditText {
    public LastInputEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public LastInputEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LastInputEditText(Context context) {
        super(context);
    }

    public void onSelectionChanged(int selStart, int selEnd) {
        super.onSelectionChanged(selStart, selEnd);//保证光标始终在最后面
        if (selStart == selEnd) {//防止不能多选
            setSelection(getText().length());
        }
    }
}

2 包装的六个输入框的 LinearLayout

public class PasswordView extends LinearLayout {
    private LastInputEditText etone;
    private LastInputEditText ettwo;
    private LastInputEditText etthree;
    private LastInputEditText etfour;
    private LastInputEditText etfive;
    private LastInputEditText etsix;
    private Context mContext;
    private View converview;

    private StringBuilder stringBuilder;
    private InputMethodManager imm;

    public PasswordView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        imm = (InputMethodManager)mContext.getSystemService(INPUT_METHOD_SERVICE);
        initView();
    }

    private void initView() {
        converview = LayoutInflater.from(mContext).inflate(R.layout.layout_password, null);
        etone = (LastInputEditText) converview.findViewById(R.id.et_one);
        ettwo = (LastInputEditText) converview.findViewById(R.id.et_two);
        etthree = (LastInputEditText) converview.findViewById(R.id.et_three);
        etfour = (LastInputEditText) converview.findViewById(R.id.et_four);
        etfive = (LastInputEditText) converview.findViewById(R.id.et_five);
        etsix = (LastInputEditText) converview.findViewById(R.id.et_six);

        etone.addTextChangedListener(textWatcher);
        ettwo.addTextChangedListener(textWatcher);
        etthree.addTextChangedListener(textWatcher);
        etfour.addTextChangedListener(textWatcher);
        etfive.addTextChangedListener(textWatcher);
        etsix.addTextChangedListener(textWatcher);

        etone.setOnKeyListener(onKeyListener);
        ettwo.setOnKeyListener(onKeyListener);
        etthree.setOnKeyListener(onKeyListener);
        etfour.setOnKeyListener(onKeyListener);
        etfive.setOnKeyListener(onKeyListener);
        etsix.setOnKeyListener(onKeyListener);

        etone.setTransformationMethod(passwordTransformationMethod);
        ettwo.setTransformationMethod(passwordTransformationMethod);
        etthree.setTransformationMethod(passwordTransformationMethod);
        etfour.setTransformationMethod(passwordTransformationMethod);
        etfive.setTransformationMethod(passwordTransformationMethod);
        etsix.setTransformationMethod(passwordTransformationMethod);
        converview.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        this.addView(converview);
    }


    private TextWatcher textWatcher = 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 s) {

            if (s.toString().length() == 1) {
                if (etone.isFocused()) {
                    ettwo.requestFocus();
                } else if (ettwo.isFocused()) {
                    etthree.requestFocus();
                } else if (etthree.isFocused()) {
                    etfour.requestFocus();
                } else if (etfour.isFocused()) {
                    etfive.requestFocus();
                } else if (etfive.isFocused()) {
                    etsix.requestFocus();
                } else if (etsix.isFocused()) {

                }
            }
        }
    };


    private PasswordTransformationMethod passwordTransformationMethod = new PasswordTransformationMethod() {
        @Override
        public CharSequence getTransformation(CharSequence source, View view) {
            return new PasswordCharSequence(source);
        }

    };

    private OnKeyListener onKeyListener = new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
                if (etsix.isFocused() && etsix.getText().length() == 0) {
                    etfive.requestFocus();
                    etfive.setText("");
                } else if (etfive.isFocused() && etfive.getText().length() == 0) {
                    etfour.requestFocus();
                    etfour.setText("");
                } else if (etfour.isFocused() && etfour.getText().length() == 0) {
                    etthree.requestFocus();
                    etthree.setText("");
                } else if (etthree.isFocused() && etthree.getText().length() == 0) {
                    ettwo.requestFocus();
                    ettwo.setText("");
                } else if (ettwo.isFocused() && ettwo.getText().length() == 0) {
                    etone.requestFocus();
                    etone.setText("");
                }
            }
            return false;
        }
    };

    class PasswordCharSequence implements CharSequence {
        private CharSequence mSource;

        public PasswordCharSequence(CharSequence source) {
            mSource = source;
        }

        @Override
        public int length() {
            return mSource.length();
        }

        @Override
        public char charAt(int index) {
            return '●';
        }

        @Override
        public CharSequence subSequence(int start, int end) {
            return mSource.subSequence(start, end);
        }
    }

    private StringBuilder getValue() {
        stringBuilder = null;
        stringBuilder = new StringBuilder();
        return stringBuilder.append(etone.getText().toString())
                .append(ettwo.getText().toString())
                .append(etthree.getText().toString())
                .append(etfour.getText().toString())
                .append(etfive.getText().toString())
                .append(etsix.getText().toString());
    }


}

布局文件就不说明了,蛮简单的
其实还是有一个问题,设计的时候一直想让其不能用手触碰获取焦点,但事键盘的伸出和收起,很难控制。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值