android auto scroll when clicked on edittext

前端 未结 4 1937
忘了有多久
忘了有多久 2020-12-10 09:42

When user click on edittext I want the screen to scroll to show the edittext in the middle of the screen. So I tried to listen to touch (setOnTouchListener, also tried with

相关标签:
4条回答
  • 2020-12-10 10:28

    Returning true from the onTouch() method indicates that the touch event is being consumed there. To allow the event to propagate through to the View's own touch listener, you need to return false;.

    To get the EditText to gain focus after the ScrollView has finished its scroll, you can post a Runnable to the ScrollView's handler to request focus on the EditText. For example:

    et_email.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ...
                scrollView.smoothScrollTo(0, et_email.getTop());
                scrollView.post(new Runnable() {
                        @Override
                        public void run() {
                            et_email.requestFocus();
                        }
                    }
                );
                return false;
            }
        }
    );
    
    0 讨论(0)
  • 2020-12-10 10:30

    First I would like to say you have to use "OnFocusListener" and then you have to use Handler like this;

    Now, When I click my edittext; keyboard opened and scrollview scrolled to top at the same time :)

    et_space.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    final Handler handler;
                    handler = new Handler();
    
                    final Runnable r = new Runnable() {
                        public void run() {
                            scrollView.smoothScrollTo(0, 500);
                            handler.postDelayed(this, 200);
                        }
                    };
                    handler.postDelayed(r, 200);
                }
            });
    
    0 讨论(0)
  • 2020-12-10 10:37

    Use this line of code on your onCreate method . It gives you auto scrolling when you clicked on EditText :

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    
    0 讨论(0)
  • 2020-12-10 10:45

    Unfortunately output program still want double click to scroll

     final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollview);
                et_space.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        scrollView.smoothScrollTo(0, 500);
                        scrollView.post(new Runnable() {
                            @Override
                            public void run() {
                                et_space.requestFocus();
    
                            }
                        });
                        return false;
                    }
                });
    
    0 讨论(0)
提交回复
热议问题