我们在一个布局中有 17 个 api、DPAD 控件和 4 个编辑文本用于 pin 输入。当用户尝试打开被锁定在应用程序中的东西时,他遇到了应该输入 4 位密码的片段。当视图打开时,我们以编程方式将视图焦点设置为第一个引脚编辑文本并显示键盘。当用户输入第一个 pin 数字时,我们以编程方式将视图焦点设置为第二个 pin edittext,用户应在其中输入第二个 pin 值,依此类推。
问题: 用户输入第一个 pin 值后,键盘焦点(突出显示)消失,而对于第二个 pin,用户应重新从开始浏览键盘。在 21 api 上,相同的代码运行良好,键盘焦点(突出显示)保持在以前的值上。当他在 17 api 上设置视图焦点到另一个视图 如何离开键盘焦点(突出显示) ?
这是代码:
public class PasswordFragment extends BaseFragment {
private final ArrayList<Disposable> disposables = new ArrayList<>();
@BindViews({R.id.passFirst, R.id.passSecond, R.id.passThird, R.id.passFour})
EditText[] pinEdits;
@BindDrawable(R.drawable.border_light)
Drawable borderLight;
@BindDrawable(R.drawable.border_white)
Drawable borderWhite;
@Inject
PrefsRepo prefsRepo;
@Inject
ToastUtil toastUtil;
@BindView(R.id.passTitle)
TextView passTitle;
@BindString(R.string.insert_pin)
String defDialogMes;
private InputMethodManager inputMethodManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
App.getAppComponent().inject(this);
inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = super.onCreateView(inflater, container, savedInstanceState);
for (EditText pinEdit : pinEdits) {
pinEdit.setOnFocusChangeListener((view, hasFocus) -> {
if (hasFocus) {
view.setBackground(borderLight);
}
});
}
disposables.add(RxTextView.textChanges(pinEdits[0]).subscribe(charSequence -> {
if (charSequence.length() == 1) {
pinEdits[1].requestFocus();
}
}));
disposables.add(RxTextView.textChanges(pinEdits[1]).subscribe(charSequence -> {
if (charSequence.length() == 1) {
pinEdits[2].requestFocus();
}
}));
disposables.add(RxTextView.textChanges(pinEdits[2]).subscribe(charSequence -> {
if (charSequence.length() == 1) {
pinEdits[3].requestFocus();
}
}));
disposables.add(RxTextView.textChanges(pinEdits[3])
.subscribe(charSequence -> {
if (charSequence.length() == 1) {
String pin = pinEdits[0].getText().toString() + pinEdits[1].getText().toString() + pinEdits[2].getText().toString() + pinEdits[3].getText().toString();
if (pin.length() == 4) {
do someting;
} else {
toastUtil.showToast(getString(R.string.pin_incorrect));
}
}
}));
return v;
}
@Override
public void onResume() {
clearEditTexts();
super.onResume();
}
private void clearEditTexts() {
for (EditText pinEdit : pinEdits) {
pinEdit.getText().clear();
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR1){
pinEdit.setOnClickListener(view -> {
showKeyBoard(pinEdit);
});
}
}
setFocusOnFirst();
}
private void setFocusOnFirst() {
pinEdits[0].setBackground(borderLight);
pinEdits[0].requestFocus();
pinEdits[0].postDelayed(() -> {
showKeyBoard(pinEdits[0]);
}, 50);
}
@Override
public void onDestroy() {
super.onDestroy();
for (Disposable disposable : disposables) {
disposable.dispose();
}
}
@Override
protected int getLayoutId() {
return R.layout.fragment_password;
}
private void showKeyBoard(EditText editText){
inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
}