首页 > 解决方案 > Next 按钮跳过 Date Picker 字段,只关注 EditText

问题描述

我有一个包含一些editTexts 和datePiker 的表单。当我按下下一个时填写表单,它专注于下一个editText但是当editText之间有日期选择器或微调器时,它们会被跳过并且它们之后的下一个editText被设置为焦点。有人可以帮我解决这个问题吗?

标签: androidform-fields

解决方案


editTextBefore.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_NEXT) {
            hideKeyboard();
            textView.clearFocus();
            spinner.requestFocus();
            spinner.performClick();
        }
        return true;
    }
});

或者

添加此行以专注于微调器

Spinner spinner = (Spinner) findViewById(R.id.my_spinner); 
spinner.setFocusable(true); 
spinner.setFocusableInTouchMode(true);
spinner.requestFocus();

您还需要隐藏键盘,您可以使用以下方法执行此操作

private void hideKeyboard() {
    InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
            InputMethodManager.HIDE_NOT_ALWAYS);
}

推荐阅读