Stop ScrollView from auto-scrolling to an EditText

后端 未结 21 1290
长发绾君心
长发绾君心 2020-11-30 23:17

Seems to be a common problem without a great solution that I have found. Goal is to stop a ScrollView from auto-scrolling to an EditText (or any vi

相关标签:
21条回答
  • 2020-11-30 23:40

    Another version of thomas88wp's code:

    ScrollView scroll = (ScrollView)getActivity().findViewById(R.id.scrollView_addNewBill);
        scroll.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {        
                View focussedView = getCurrentFocus(); 
                if( focussedView != null ) focussedView.clearFocus();
                    
                return false;
        }
    });
    
    0 讨论(0)
  • 2020-11-30 23:41

    We can write a custom ScrollView and override the onScrollChanged method and clear the focus from the focused view and optionally hide the keyboard.

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        View v = getFocusedChild();
        if (v != null) {
            InputMethodManager imm = (InputMethodManager) getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            v.clearFocus();
        }
        super.onScrollChanged(l, t, oldl, oldt);
    }
    
    0 讨论(0)
  • 2020-11-30 23:43

    I made a test project to experiment with the various solutions if anyone wants to play with it. https://github.com/marchold/EditText-ErrorPopup-Scroll-View

    0 讨论(0)
提交回复
热议问题