我试图使Pin进入页面,并且创建了4个EditText,并且创建了一个功能,该功能在我的 Activity 下面。我的问题是,当我单击Backspace按钮时,我想将当前EditText集中到Before(Back)EditText,但是Key监听器不起作用。
逻辑很简单,当用户输入一个数字来编辑文本时,它失去焦点,而下一个编辑文本成为焦点,然后就这样了。但是问题是当我单击退格键时,我想在edittext再次输入数字之前返回。我试图将keyListener插入beforeTextChange内,但无法正常工作。

 private void SetTextChange(final EditText etCurrent, final EditText etForward,final EditText etBack, final boolean isLast, final int currentPosition)
{

    etCurrent.addTextChangedListener(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) {

            if (etCurrent.getText().length() == 1 && !isLast ) {
                etCurrent.setTypeface(null, Typeface.NORMAL);
                etCurrent.clearFocus();
                etForward.requestFocus();
                etForward.setCursorVisible(true);

                etCurrent.getBackground().setColorFilter(getResources().getColor(R.color.lavender_indigo), PorterDuff.Mode.SRC_ATOP);
            } else if (etCurrent.length() == 0) {
                etCurrent.getBackground().setColorFilter(getResources().getColor(R.color.french_gray), PorterDuff.Mode.SRC_ATOP);
                etCurrent.setTypeface(null, Typeface.SANS_SERIF.getStyle());

                etCurrent.setTypeface(null, Typeface.NORMAL);
                etCurrent.clearFocus();
                etBack.requestFocus();
                etBack.setCursorVisible(true);

            }


            if (etCurrent.length() != 0) {
                Integer currentKey = Integer.parseInt(etCurrent.getText().toString());
                keyList.set(currentPosition, currentKey);
            } else
                keyList.set(currentPosition, -1);

            if (keyList.size() > 3)
                showToast(keyList.get(0) + " " + keyList.get(1) + " " + keyList.get(2) + " " + keyList.get(3));

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    /*curText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(View arg0, boolean arg1) {
            curText = etCurrent;
            backText = etBack;
            curText.setText("");
        }

        });*/
    }
这也是我的EditText示例xml之一(其他相同)
  <EditText
        android:layout_column="1"
        android:layout_columnWeight="1"
        android:id="@+id/etActivationDigit1"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:textSize="80dp"
        android:textAlignment="center"
        android:gravity="center_horizontal"
        android:hint="*"
        android:textColorHint="@color/french_gray"
        android:backgroundTint="@color/french_gray"
        android:fontFamily="sans-serif"
        android:textColor="@color/perfume"
        android:maxLength="1"
        android:inputType="number"
         />
当用户输入错误的数字时,我只是想擦除并重新聚焦。注意:当您填写所有字段时,您会看到可以删除并返回,但我想返回到密码的中间。谢谢你。
  etCurrent.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if(etCurrent.getText().toString() == "" && keyCode == event.KEYCODE_DEL)
            {
                etCurrent.setText("");
                etCurrent.clearFocus();
                etBack.requestFocus();
                etBack.setText("");
            }
            return false;
        }
    });
当我触摸Backspace时,不会触发我的Backspace监听器,但会触发所有其他字符。

最佳答案

这是我的4位数pin码 Activity 的工作方式。

private void setupInputListeners() {
    verificationCode1EditText.addTextChangedListener(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) {
            if (s.length() == 1) {
                verificationCode2EditText.requestFocus(View.FOCUS_DOWN);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

    verificationCode2EditText.addTextChangedListener(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) {
            if(count == 0) {
                verificationCode1EditText.requestFocus(View.FOCUS_DOWN);
            } else {
                if (s.length() == 1) {
                    verificationCode3EditText.requestFocus(View.FOCUS_DOWN);
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

    verificationCode3EditText.addTextChangedListener(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) {
            if(count == 0) {
                verificationCode2EditText.requestFocus(View.FOCUS_DOWN);
            } else {
                if (s.length() == 1) {
                    verificationCode4EditText.requestFocus(View.FOCUS_DOWN);
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

    verificationCode4EditText.addTextChangedListener(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) {
            if(count == 0) {
                verificationCode3EditText.requestFocus(View.FOCUS_DOWN);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}

关于Android如何在单击退格键时聚焦另一个edittext,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33385755/

10-14 11:00