【问题标题】:Cyclical replacement the content of EditText with the limited length用有限长度循环替换EditText的内容
【发布时间】:2020-01-24 17:33:43
【问题描述】:

例如,我有两个字符的长度限制的 EditText。当输入第一个和第二个字母时就可以了。但是当我们尝试输入第三个字母时,第一个字母应该用它代替。下一个字母应替换第二个字母,以此类推。我该怎么做。

【问题讨论】:

  • 哪个变体正确显示了预期的行为? 1. 输入a -> a, 输入b -> ab, 输入c -> bc, 输入d -> cd, 输入e -> de 2. 输入a -> a, 输入b -> ab, 输入c -> cb , 输入 d -> cd, 输入 e -> ed
  • 预期行为:a->a, b->ab, c->cb, d->cd

标签: android android-edittext


【解决方案1】:

试试这个

editText.addTextChangedListener(new TextWatcher() {
        private int charLimit = 5;
        private int position = 5;
        private String newSequence;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if (s.length() > charLimit ) {

                if (position == charLimit) {
                    newSequence = s.subSequence(s.length()-1, s.length()).toString() +
                            s.subSequence(1, charLimit);
                        position = 1;

                } else {
                    position++;
                    newSequence = s.subSequence(0, position).toString() +
                            s.subSequence(position+1, s.length());
                }

                editText.setText(null);
                editText.setText(newSequence);
                editText.setSelection(position);
            }

        }

        @Override
        public void afterTextChanged(Editable s) {

       }
    });

【讨论】:

    【解决方案2】:

    尝试在编辑文本上使用 TextWatcher 以实现目标

    editText.addTextChangedListener(new TextWatcher() {
    
            private int lastModifiedIndex = 1;
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 2) {
                    char toReplace = s.charAt(s.length() - 1);
                    if (lastModifiedIndex == 0) {
                        editText.setText("" + s.charAt(lastModifiedIndex) + toReplace);
                        lastModifiedIndex = 1;
                        editText.setSelection(s.length());
                    } else {
                        editText.setText("" + toReplace + s.charAt(lastModifiedIndex));
                        lastModifiedIndex = 0;
                        editText.setSelection(s.length());
                    }
                } else {
                    lastModifiedIndex = 1;
                }
            }
    
            @Override
            public void afterTextChanged(Editable s) {
    
            }
        });
    

    【讨论】:

    • 不幸的是,代码 sn-p 不能正常工作。此外,由于 EditText 长度限制只有两个符号,“if”语句将无法访问。
    • 我明白了。然后你应该从 XML 中移除这个限制,只留下文本观察器 - if 语句也会做这个限制
    • IndexOutOfBoundsException: editText.setSelection(s.length()) ->editText.setSelection(s.length()-1)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多