我有4个edittext,输入类型为numberPassword,每个最大长度为1.一旦输入数字,我就会以编程方式将焦点更改为下一个字段。我希望一旦焦点改变到下一个字段就会掩盖数字,但是当我在3时然后只有1被掩盖或者当我在4然后3和2时,发生的事情是不可预测的。被掩盖但不是1,有时没有一个被掩盖,但是当我手动地专注于任何场地时它会被掩盖。我希望在焦点更改为下一个字段时也能保持一致性,应该屏蔽最后一个字段。
Xml布局:
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1">
<EditText
android:id="@+id/et_mpin_1"
android:layout_width="51dp"
android:layout_height="44dp"
android:ems="10"
android:gravity="center_horizontal"
android:inputType="numberPassword"
android:maxLength="1"
android:maxLines="1"
android:imeOptions="actionNext"
android:focusableInTouchMode="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1">
<requestFocus />
</EditText>
<EditText
android:id="@+id/et_mpin_2"
android:layout_width="51dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:ems="10"
android:gravity="center_horizontal"
android:inputType="numberPassword"
android:maxLength="1"
android:maxLines="1"
android:imeOptions="actionNext"
app:layout_constraintBottom_toBottomOf="@+id/et_mpin_1"
app:layout_constraintLeft_toRightOf="@+id/et_mpin_1"
app:layout_constraintTop_toTopOf="@+id/et_mpin_1"
app:layout_constraintVertical_bias="0.0"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintTop_creator="1" />
<EditText
android:id="@+id/et_mpin_4"
android:layout_width="51dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:ems="10"
android:gravity="center_horizontal"
android:inputType="numberPassword"
android:maxLength="1"
android:maxLines="1"
android:imeOptions="actionDone"
app:layout_constraintBottom_toBottomOf="@+id/et_mpin_1"
app:layout_constraintLeft_toRightOf="@+id/et_mpin_3"
app:layout_constraintTop_toTopOf="@+id/et_mpin_1"
app:layout_constraintVertical_bias="0.0"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintTop_creator="1" />
<EditText
android:id="@+id/et_mpin_3"
android:layout_width="51dp"
android:layout_height="0dp"
android:layout_marginEnd="59dp"
android:layout_marginStart="59dp"
android:ems="10"
android:gravity="center_horizontal"
android:inputType="numberPassword"
android:maxLength="1"
android:maxLines="1"
android:imeOptions="actionNext"
app:layout_constraintBottom_toBottomOf="@+id/et_mpin_1"
app:layout_constraintLeft_toLeftOf="@+id/et_mpin_2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/et_mpin_1"
app:layout_constraintVertical_bias="1.0"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
</android.support.constraint.ConstraintLayout>
Java代码:
et_mpin1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// et_mpin1.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
// et_mpin1.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// checking before only because for max lenght 1 start is always going to be 0
if (before == 0) {
et_mpin2.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
et_mpin2.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) {
// checking before only because for max lenght 1 start is always going to be 0
if (before == 0) {
et_mpin3.requestFocus();
} else {
et_mpin1.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
et_mpin3.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) {
// checking before only because for max lenght 1 start is always going to be 0
if (before == 0) {
et_mpin4.requestFocus();
} else {
et_mpin2.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
et_mpin4.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) {
// checking before only because for max lenght 1 start is always going to be 0
if (before == 0) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(getContext().INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et_mpin4.getWindowToken(), 0);
} else {
et_mpin3.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
答案 0 :(得分:1)
强行隐藏密码
android.provider.Settings.System.putInt(this.getContentResolver(),android.provider.Settings.System.TEXT_SHOW_PASSWORD, 0);
注意:它不会显示任何字符,直接DOT将是 在您的视图中显示没有任何延迟。