停止 ScrollView 自动滚动到 EditText [英] Stop ScrollView from auto-scrolling to an EditText

查看:20
本文介绍了停止 ScrollView 自动滚动到 EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎是一个常见问题,但我没有找到很好的解决方案.目标是阻止 ScrollView 自动滚动到具有焦点的 EditText(或任何视图).

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 view for that matter) that has focus.

ScrollView 中有一堆视图(Buttons、TextViews 等),其中一个是 >编辑文本.单击 ScrollView 中的按钮后,ScrollView 会向下滚动到 EditText(它的屏幕外).这是不希望的,因为您不希望将其他元素滚动到屏幕之外.

You have a bunch of views (Buttons, TextViews, etc) in an ScrollView, one of which is an EditText. Upon clicking say a Button within the ScrollView, the ScrollView scrolls down to the EditText (its off screen). This is not desired, as there are other elements that you don't want scrolled off the screen.

现在我可以通过在 ScrollView 中添加其他可聚焦元素来阻止在屏幕首次显示时发生这种情况.但是,普遍的问题仍然存在.用户手动向下滚动到 EditText,输入一些数字,然后向上滚动到顶部(EditText 现在离开屏幕),他们单击 ScrollView 中的按钮,你猜怎么着?ScrollView 向下滚动到那个该死的 EditText.

Now I can stop this from happening when the screen first shows by having other focusable elements in the ScrollView. However, the general problem still exists. The user scrolls down manually to the EditText, enters some numbers, then scrolls up to the top (EditText off screen now), they click a button in the ScrollView, and guess what? The ScrollView scrolls down to that darn EditText.

我正在考虑扩展 ScrollView 并覆盖其中的一些方法,例如 findFocusableViewInBounds,但我觉得我只会让自己陷入更多麻烦.

I'm thinking about extending the ScrollView and overriding some of the methods there like findFocusableViewInBounds, but I have a feeling I'll just be getting myself into more trouble.

如果可以,请提供帮助.

Please help if you can.

我尝试过在 ScrollView 顶部设置 0 高度 EditText,将 Next Focusable 元素属性添加到 ScrollView 等.我想一个hack"可能是让 EditText 在虚拟或手动键盘被隐藏或其他情况下失去焦点.

I've played around with things like having an 0 height EditText at the top of my ScrollView, adding Next Focusable element properties to the other items in the ScrollView, etc. I suppose one "hack" might be to get the EditText to lose focus when the virtual or manual keyboard gets hidden or something.

推荐答案

在为这个问题苦苦挣扎了一段时间后,我找到了一个似乎可行而又不会太难看的解决方案.首先,确保任何 ViewGroup(直接)包含您的 EditTextdescendantFocusability 设置为Before Descendants",focusable 设置为true"并且 focusableInTouchMode 设置为true".这将不是 ScrollView 本身,而是您拥有各种视图的内部布局.接下来添加一个 onTouchListener 到您的 ScrollView 中,当它被触摸时,它会从 EditText 中移除焦点,如下所示:

After struggling with that problem for quite some time, I've found a solution that seems to work without being too ugly. First, make sure that whatever ViewGroup (directly) contains your EditText has descendantFocusability set to "Before Descendants," focusable set to "true" and focusableInTouchMode set to "true." This will not be the ScrollView itself, but the layout inside where you have your various views. Next add an onTouchListener to your ScrollView that removes focus from the EditText whenever it is touched, like so:

ScrollView scroll = (ScrollView)findViewById(R.id.scrollView1);
scroll.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (myEditText.hasFocus()) {
            myEditText.clearFocus();
        }
        return false;
    }
});

如果这不能解决问题,请告诉我.应该发生的是布局而不是 EditText 获得焦点,因此不应发生滚动.

Tell me if that doesn't fix it. What should happen is that the Layout gets focus instead of the EditText, so no scrolling should happen.

这篇关于停止 ScrollView 自动滚动到 EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆