Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

android - Spinner does not get focus

I'm using 4 EditText fields and 2 spinners in an activity. The order of these components are 2 EditText, then 2 spinners and then 2 EditText fields.

The problem occurs when I transfer focus (with the help of soft keyboard next button) from EditText to spinner, spinner does not get the focus and the focus is transferred to the next EditText field that was placed after the spinners.

I have used requestfocus() on spinner, but it did not work.

How do I make sure the spinner gets focus?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I managed to find a solution other then repositioning my Spinner. In the EditText before the spinner, add this listener:

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;
    }
});

You also need to add these line to able spinner to get focus:

spinner.setFocusable(true); // can be done in XML preferrable

My hideKeyboard function was just a visual detail that I wanted to add so the keyboard get hidden:

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

Hope I have helped in this tricky question.
The flag InputMethodManager.HIDE_NOT_ALWAYS can be found in the documentation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...