Set focus on spinner when selected in android

前端 未结 4 1898

I have a vertical scrolling layout with multiple EditText and Spinner controls. Problem is when i choose any spinner option after selection the screen scrolls back to the l

4条回答
  •  清歌不尽
    2021-01-05 10:47

    I solved the problem with the help of Ryderz answer. here is the code :

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    scrollView = (ScrollView) findViewById(R.id.sv_main);
        scrollView.setOnTouchListener(new OnTouchListener() {
            // to solve focus problem on scrolling
            public boolean onTouch(View v, MotionEvent event) {
    
                IBinder windowToken = null;
                if (myEditText1.hasFocus()) {
                    myEditText1.clearFocus();
                    windowToken = myEditText1.getWindowToken();
                }
                if (myEditText2.hasFocus()) {
                    myEditText2.clearFocus();
                    windowToken = myEditText2.getWindowToken();
                }
                if (myEditText3.hasFocus()) {
                    myEditText3.clearFocus();
                    windowToken = myEditText3.getWindowToken();
                }
                if (windowToken != null) {
                    imm.hideSoftInputFromWindow(windowToken, 0);
                }
                scrollView.requestFocusFromTouch();
                return false;
            }
        });
    

    Then I set the android:focusable="true" for my textViews so that on scroll when focus is removed from editText then the Textviews can be picked for focus. In that way the user doesn't see any focused control on screen.

提交回复
热议问题