开发者

Scroll to form element

I have a form with lots of fields and when I开发者_运维技巧 press the "submit" button, I'd like the scrollview to jump to the position where the unanswered field is located. Is there a method other than scrollto(x,y) to do this or such coordinates must always be mapped to the form's elements somehow ( something like (form element).getCoordinates() )?

Thanks


This should work -

view.setFocusableInTouchMode(true);
view.requestFocus();
view.requestRectangleOnScreen(viewRectangle);

You can always take a look at the View class.

P.S. The unanswered field will be selected but I'm not sure that if it is out of the screen the Activity will scroll to the requested position.


ScrollViews do have predefined methods scrollTo() & scrollBy() for such functionality.

I believe you are having issues in getting co-ordinates. So I suggest store height of each view (that consumes vertical space)by using View.getHeight method & store heights in a Map collection . Below is the code snippet I tried

    scrollView.post(new Runnable() {
                 public void run() {
               Integer emailTextViewHeight = (Integer)hashMap.get("EmailTextViewHeight");
            scrollView.scrollBy(0, emailTextViewHeight);//scrolls to emailTextView
                 }
             });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜