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

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

问题描述

似有若无,我已经找到了一个很好的解决方案的常见问题。目标是阻止从滚动型自动滚动到一个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.

您有一堆的意见(按钮,textviews等)在滚动视图,其中之一是一个EditText。在单击说,滚动型中的按钮,滚动视图向下滚动到的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.

现在我可以阻止这种情况发生时,屏幕上首先显示通过在滚动视图其他可聚焦元素。但是,一般的问题依然存在。用户向下滚动手动到EditText上,输入一些数字,然后滚动到顶部(EditText上现学现用的屏幕),他们点击滚动型一个按钮,你猜怎么着?滚动视图向下滚动到该死的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.

我在想继承滚动视图,并覆盖部分的私有方法有像findFocusableViewInBounds,但我有一种感觉,我就可以让自己变成更大的麻烦。

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

如果你能请帮助。

我打得周围的东西就像一个0高度的EditText我的滚动型的顶部,在滚动型加下聚焦元素属性的其他项目,等等。我想一黑客可能会得到的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(直接)包含您的EditText已经DescendantFocusability设置为后人之前​​,可调焦设置为true和FocusableInTouchMode设置为true。这会不会是滚动型本身,但如果你有你的各种意见里面的布局。接下来的onTouchListener添加到您的滚动型的焦点离开的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.

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

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