我想用EditText设计一个OTP屏幕

时间:2018-07-29 11:14:29

标签: android android-edittext one-time-password

我打算设计一个带有6个EditTexts的OTP屏幕,所以我已经在所有editTexts中实现了TextWatcher。因此,它前进到每个editText,并且我还基于editText长度实现了向后移动的逻辑,但是如果用户输入了错误的otp,并且如果用户在单击键盘上的删除键时进行了特定的editText,它将转到先前的editText,但是不应返回到先前的editText,而应保留在相同的editText中。

private void setEventsForEditText() {
   editFirstOTPDigit.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) {
           if (charSequence.length() == 1) {
               editSecondOTPDigit.requestFocus();
           }
       }

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

   editSecondOTPDigit.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) {
           if (charSequence.length() == 1) {
               editThirdOTPDigit.requestFocus();
           } else {
               editSecondOTPDigit.requestFocus();
           }

       }

       @Override
       public void afterTextChanged(Editable editable) {

       }
   });

   editThirdOTPDigit.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) {
           if (charSequence.length() == 1) {
               editFourthOTPDigit.requestFocus();
           } else {
               editSecondOTPDigit.requestFocus();
           }
       }

       @Override
       public void afterTextChanged(Editable editable) {

       }
   });

   editFourthOTPDigit.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) {
           if (charSequence.length() == 1) {
               editFifthOTPDigit.requestFocus();
           } else {
               editThirdOTPDigit.requestFocus();
           }

       }

       @Override
       public void afterTextChanged(Editable editable) {

       }
   });

   editFifthOTPDigit.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) {
           if (charSequence.length() == 1) {
               editSixthOTPDigit.requestFocus();
           } else {
               editFourthOTPDigit.requestFocus();
           }
       }

       @Override
       public void afterTextChanged(Editable editable) {

       }
   });

   editSixthOTPDigit.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) {
           if (charSequence.length() == 1) {
               buttonVerifyOTP.setVisibility(View.VISIBLE);
               hideKeyboard();
           } else {
               editFifthOTPDigit.requestFocus();
           }
       }

       @Override
       public void afterTextChanged(Editable editable) {

       }
   });

1 个答案:

答案 0 :(得分:0)

尚未测试过,但是您可以尝试以下操作:

创建一些标语

         private boolean isWrondOtp;

在OTP验证后将其设置为true

您的文本监视程序将看起来像这样

editSecondOTPDigit.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) {
            if (charSequence.length() == 1) {
                editThirdOTPDigit.requestFocus();
            } else {
                if (!isWrondOtp)
                    editSecondOTPDigit.requestFocus();
            }

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });