2

在 android 中,我需要使用四个单个数字字段的密码,并且我使用了四个 edittext 视图。如果我们输入 4 个数字,它会自动填充四个字段。我已经通过使用 addTextChangedListener 完成了这项工作。但是除了问题之外,我的密码文件没有被点替换。我认为由于使用 requestfocus() 到下一个字段,前一个字段没有转换点。请帮我。

  passInput1.addTextChangedListener(new CustomTextWatcher(passInput1, passInput2));
  passInput2.addTextChangedListener(new CustomTextWatcher(passInput2, passInput3));
  passInput3.addTextChangedListener(new CustomTextWatcher(passInput3, passInput4));

我的 CustomWatcher 如下所示。

public class CustomTextWatcher implements TextWatcher {
      private EditText currentEditTextFiledId;
      private EditText nextEditTextFiledId;

         public CustomTextWatcher(EditText currentEditTextFiledId, EditText  nextEditTextFiledId) { 
        this.currentEditTextFiledId = currentEditTextFiledId;
        this.nextEditTextFiledId = nextEditTextFiledId;
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
         currentEditTextFiledId.setTransformationMethod(PasswordTransformationMethod.getInstance());
    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
         Integer textlength1 = currentEditTextFiledId.getText().length();


         if (textlength1 >= 1) { 

             nextEditTextFiledId.requestFocus();

         }
    }

    public void afterTextChanged(Editable s) {

    }
}
4

3 回答 3

1

我找到了一种有趣的方法,只需将焦点移动到应该首先屏蔽的任何字段,然后再将焦点移动到下一个字段:

pinBox1.addTextChangedListener(new CustomTextWatcher(pinBox1, pinBox2, pinBox2));
pinBox2.addTextChangedListener(new CustomTextWatcher(pinBox2, pinBox3, pinBox1));
pinBox3.addTextChangedListener(new CustomTextWatcher(pinBox3, pinBox4, pinBox2));
pinBox4.addTextChangedListener(new CustomTextWatcher(pinBox4, pinBox4, pinBox3));

所以我传入另一个参数,以便更改字段,我们首先请求焦点:

public class CustomTextWatcher implements TextWatcher {
    private EditText currentEditTextFieldId;
    private EditText nextEditTextFieldId;
    private EditText editTextFieldToChange;

    public CustomTextWatcher(EditText currentEditTextFieldId, EditText  nextEditTextFieldId, EditText editTextFieldToChange) { 
        this.currentEditTextFieldId = currentEditTextFieldId;
        this.nextEditTextFieldId = nextEditTextFieldId;
        this.editTextFieldToChange = editTextFieldToChange;
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        currentEditTextFieldId.setTransformationMethod(PasswordTransformationMethod.getInstance());
    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Integer textlength1 = currentEditTextFieldId.getText().length();


        if (textlength1 >= 1) { 
            editTextFieldToChange.requestFocus();
            nextEditTextFieldId.requestFocus();
        }
    }

    public void afterTextChanged(Editable s) {

    }
}
于 2013-05-14T15:39:58.610 回答
0

即使在密码模式下,Android 也总是显示最后输入的符号(因为按钮很小,很容易按错)。一般来说,四个数字字段听起来是个坏主意。如果用户按下退格键会发生什么?

我会在http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType中使用单个密码字段android:inputType="number"

如果您真的想重新发明轮子,您可能有 4 个只读文本字段和 10 个用于输入数字的大按钮——至少,您将控制它们的行为。

于 2013-02-07T06:34:44.897 回答
0

Post Runnable to the Current EditText. You need to make current EditText to wait until it convert enterted text to dot,then call requestFocus().

currentEditTextFiledId.post(new Runnable(){
     Integer textlength1 = currentEditTextFiledId.getText().length();
     if (textlength1 >= 1) { 
         nextEditTextFiledId.requestFocus();
     }
});
于 2014-04-12T12:16:24.963 回答