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

时间:2018-11-22 14:56:24

标签: android android-edittext onfocus

我有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不会将自身掩盖为点/星号,而是将焦点转移到et2。
但是,只要我再次点击它,它就会掩盖自己。

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

1 个答案:

答案 0 :(得分:1)

尝试这种方式

  

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
        }
    }