从第一个编辑文本到第二个编辑文本,输入掩码不起作用

from 1st edittext going to 2nd edittext, input mask not working

我有 6 个编辑文本(将用于 OTP)
每个edittext将只允许1个字符,然后转移到另一个edittext

示例代码如下:

et1.requestFocus()

et1.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence s, int start,int before, int count {
    if(et1.getText().toString().length()==size){
        et2.requestFocus();
    }
}
public void beforeTextChanged(CharSequence s, int start,
                int count, int after) {}

public void afterTextChanged(Editable s) {
     et1.setTransformationMethod(new PasswordTransformationMethod());
}

});

发生的事情是,et1 没有将自己伪装成 dot/asterisk,而是将焦点转移到 et2。
但每当我再次点击它时,它就会掩盖自己。

编辑:已经在我的 XML 中添加了 inputType="numberPassword" 但仍然无法正常工作

试试这个方法

xml

<LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal">

                <EditText
                    android:id="@+id/otp_first_et"
                    android:layout_width="45sp"
                    android:layout_height="45sp"
                    android:layout_marginBottom="10dp"
                    android:digits="0123456789"
                    android:drawablePadding="7dp"
                    android:gravity="center"
                    android:inputType="number"
                    android:maxLength="1"
                    android:paddingLeft="10dp"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@drawable/bg_edit_text_cursor" />

                <EditText
                    android:id="@+id/otp_second_et"
                    android:layout_width="45sp"
                    android:layout_height="45sp"
                    android:layout_marginBottom="10dp"
                    android:digits="0123456789"
                    android:drawablePadding="7dp"
                    android:gravity="center"
                    android:inputType="number"
                    android:maxLength="1"
                    android:paddingLeft="10dp"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@drawable/bg_edit_text_cursor" />

                <EditText
                    android:id="@+id/otp_third_et"
                    android:layout_width="45sp"
                    android:layout_height="45sp"
                    android:layout_marginBottom="10dp"
                    android:digits="0123456789"
                    android:drawablePadding="7dp"
                    android:gravity="center"
                    android:inputType="number"
                    android:maxLength="1"
                    android:paddingLeft="10dp"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@drawable/bg_edit_text_cursor" />

                <EditText
                    android:id="@+id/otp_fourth_et"
                    android:layout_width="45sp"
                    android:layout_height="45sp"
                    android:layout_marginBottom="10dp"
                    android:digits="0123456789"
                    android:drawablePadding="7dp"
                    android:gravity="center"
                    android:imeOptions="actionDone"
                    android:inputType="number"
                    android:maxLength="1"
                    android:paddingLeft="10dp"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@drawable/bg_edit_text_cursor" />
            </LinearLayout>

java

EditText otpEditTextFirst, otpEditTextSecond, otpEditTextThird, otpEditTextFourth;

otpEditTextFirst = (EditText) findViewById(R.id.otp_first_et);
        otpEditTextSecond = (EditText) findViewById(R.id.otp_second_et);
        otpEditTextThird = (EditText) findViewById(R.id.otp_third_et);
        otpEditTextFourth = (EditText) findViewById(R.id.otp_fourth_et);


otpEditTextFirst.addTextChangedListener(new GenericTextWatcher(otpEditTextFirst));
        otpEditTextSecond.addTextChangedListener(new GenericTextWatcher(otpEditTextSecond));
        otpEditTextThird.addTextChangedListener(new GenericTextWatcher(otpEditTextThird));
        otpEditTextFourth.addTextChangedListener(new GenericTextWatcher(otpEditTextFourth));


class GenericTextWatcher implements TextWatcher {
        private View view;

        private GenericTextWatcher(View view) {
            this.view = view;
        }

        @Override
        public void afterTextChanged(Editable editable) {
            // TODO Auto-generated method stub
            String text = editable.toString();
            switch (view.getId()) {

                case R.id.otp_first_et:

                    if (text.length() == 1)
                        otpEditTextSecond.requestFocus();


                    break;
                case R.id.otp_second_et:

                    if (text.length() == 1)
                        otpEditTextThird.requestFocus();

                    else
                        otpEditTextFirst.requestFocus();
                    break;
                case R.id.otp_third_et:

                    if (text.length() == 1)
                        otpEditTextFourth.requestFocus();

                    else
                        otpEditTextSecond.requestFocus();
                    break;
                case R.id.otp_fourth_et:
                    if (text.length() == 0)
                        otpEditTextThird.requestFocus();


                    break;
            }
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
        }
    }