文本跨越多个edittext,Android

时间:2017-06-02 07:23:20

标签: java android android-edittext keylistener textwatcher

所以,我希望用户输入他/她的车牌号码,并希望通过分离所有字符来表现得非常时尚:enter image description here

现在它由6个带有TextWatchers的EditTexts组成,可以改变每个输入的焦点。那部分工作正常,但我的问题是删除och字符。 当用户想要编辑一个字段时,他/她只需单击该视图并删除eand replace。虽然当整件事情出错时,无法一次删除所有内容,但您必须单击每个视图并手动删除。

所以我需要的是当用户在空视图上点击退格时,它应该将焦点更改为之前的那个并删除该字符,依此类推。所以所有的EditTexts都将连接在一起工作。我已经尝试过使用KeyListeners来监听退格,但这只适用于硬件键盘,而不适用于手机上的软键盘。

如果有人能指出我比另一种解决方案更好的方向,我也很高兴。

TextWatcher:

registrationPlateEditTexts是按顺序列出的所有EditText。

private TextWatcher regPlateTextWatcher = 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) {
            for (int i=registrationPlateEditTexts.size()-1; i>=0; i--){
                if (registrationPlateEditTexts.get(i).getText().length()==1 && i!=registrationPlateEditTexts.size()-1){
                    registrationPlateEditTexts.get(i+1).requestFocus();
                    return;
                }
                else if (registrationPlateEditTexts.get(i).getText().length()==1){
                    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
            }
        }
        @Override public void afterTextChanged(Editable s) {}
    };

            else if (registrationPlateEditTexts.get(i).getText().length()==1){
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }
    }
    @Override public void afterTextChanged(Editable s) {}
};`

1 个答案:

答案 0 :(得分:0)

我按下了4个自定义编辑文本,其中包含选中的属性。以下是侦听器和CustomEditText元素

mCodeFourEt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                mConfirmBtn.performClick();
                return true;
            }
            return false;
        }


    });


    mCodeTwoEt.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DEL) {
                String text = mCodeTwoEt.getText().toString();
                if (text.length() == 0) {
                    mCodeOneEt.requestFocus();
                    mCodeOneEt.selectAll();
                    return true;
                }
            }

            return false;
        }
    });

    mCodeThreeEt.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DEL) {
                String text = mCodeThreeEt.getText().toString();
                if (text.length() == 0) {
                    mCodeTwoEt.requestFocus();
                    mCodeTwoEt.selectAll();
                    return true;
                }
            }

            return false;
        }
    });

    mCodeFourEt.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DEL) {
                String text = mCodeFourEt.getText().toString();
                if (text.length() == 0) {
                    mCodeThreeEt.requestFocus();
                    mCodeThreeEt.selectAll();
                    return true;
                }

            }

            return false;
        }
    });

    mCodeOneEt.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 (mCodeOneEt.getText().toString().length() > 0) {
                mCodeTwoEt.requestFocus();
            }

        }
    });

    mCodeTwoEt.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 (mCodeTwoEt.getText().toString().length() > 0) {
                mCodeThreeEt.requestFocus();
            }

        }
    });

    mCodeThreeEt.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 (mCodeThreeEt.getText().toString().length() > 0) {
                mCodeFourEt.requestFocus();
            }

        }
    });

    mCodeFourEt.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) {

        }
    });

现在这里是自定义edittext类

public class CustomEditText extends android.support.v7.widget.AppCompatEditText {


public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

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

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


@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    return new ZanyInputConnection(super.onCreateInputConnection(outAttrs),
            true);
}

private class ZanyInputConnection extends InputConnectionWrapper {

    public ZanyInputConnection(InputConnection target, boolean mutable) {
        super(target, mutable);
    }

    @Override
    public boolean sendKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN
                && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
            // Un-comment if you wish to cancel the backspace:
            // return false;
        }
        return super.sendKeyEvent(event);
    }


    @Override
    public boolean deleteSurroundingText(int beforeLength, int afterLength) {
        // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace
        if (beforeLength == 1 && afterLength == 0) {
            // backspace
            return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
                    && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
        }

        return super.deleteSurroundingText(beforeLength, afterLength);
    }

}
}

只需在XML中使用此自定义类,并参阅上面的示例,该示例适用于4个编辑文本。