ScrollView 禁用焦点移动 [英] ScrollView disable focus move

查看:34
本文介绍了ScrollView 禁用焦点移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的输入表单;它是一个垂直的 LinearLayout,在 ScrollView 中有 EditTexts.

I have a simple input form; it's a vertical LinearLayout with EditTexts inside a ScrollView.

<ScrollView android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
            <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:padding="10dip"
                  android:gravity="center_vertical"
                  android:orientation="horizontal">
                  <TextView style="@style/Text"
                         android:text="Name"/>
                  <EditText style="@style/EditBox"/>
            </LinearLayout>
            <View style="@style/Divider"/>
            <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:padding="10dip"
                  android:gravity="center_vertical"
                  android:orientation="horizontal">
                  <TextView style="@style/Text"
                         android:text="Password"/>
                  <EditText style="@style/EditBox"/>
            </LinearLayout>               
            ... 
     </LinearLayout>
</ScrollView>

当用户滚动表单时,它会自动将焦点移动到可见的 EditText.是否可以禁用此类行为并始终关注当前通过触摸选择的 EditText?

When the user scrolls the form, it automatically moves its focus to the visible EditText. It is possible to disable such behavior and always keep focus on the EditText currently selected by touch?

我知道这可能是一项功能,但我需要禁用它.

I understand that this may be a feature, but I need to disable it.

谢谢!

推荐答案

我只是想分享我的解决方案.即使其他一些答案的评论指出您不能覆盖此行为,但事实并非如此.一旦您覆盖了 onRequestFocusInDescendants() 方法,此行为就会停止.因此,只需创建您的 ScrollView 扩展即可:

Just thought I'd share my solution to this. Even though some of the other answer's comments state that you cannot override this behavior, that is not true. This behavior stops as soon as you override the onRequestFocusInDescendants() method. So simply create your ScrollView extension to do this:

public class NonFocusingScrollView extends ScrollView {

  public NonFocusingScrollView(Context context) {
    super(context);
  }

  public NonFocusingScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public NonFocusingScrollView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  @Override
  protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
    return true;
  }

}

你已经完成了.ScrollView 不会再干扰你的注意力.

And you're done. The ScrollView will mess with your focus no more.

这篇关于ScrollView 禁用焦点移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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