我们在一个布局中有17个api,DPAD控制和4个edittexts用于引脚输入。当用户尝试打开锁定在应用程序中的某些东西时,他会面对片段,他应该输入4位数的针脚。打开视图时,我们以编程方式将查看焦点设置为第一个图钉编辑文本并显示键盘。当用户输入第一个引脚数字时,我们以编程方式将视图焦点设置为第二个引脚edittext,用户应输入第二个引脚值,依此类推。
问题: 用户输入第一个引脚值后键盘焦点(突出显示)消失,对于第二个引脚,用户应该从头再次开始键盘。 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);
}
}