滚动型禁用焦点移到移到、焦点

由网友(最凉薄不过人心)分享简介:您好我有简单的输入形式,它与内滚动型EditTexts垂直LinearLayot。HelloI have simple input form, it's vertical LinearLayot with EditTexts inside ScrollView.您好 我有简单的输入形式,它与内滚动型EditTexts垂直LinearLayot。

Hello I have simple input form, it's vertical LinearLayot with EditTexts inside 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 user scrolls form, it automatically move focus to visible EditText. It is possible to disable such behavior and always keep focus on EditText selected by touch?

据我所知,这可能是它的一个特点,但我需要禁用它。

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

谢谢!

推荐答案

只是想我会分享我解决这个。尽管其他一些答案的评论状态,你不能覆盖这个行为,这是不正确的。这种行为一旦你覆盖 onRequestFocusInDescendants()方法停止。所以,简单地创建滚动型的扩展要做到这一点:

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;
  }

}

和你就大功告成了。该滚动型会惹你的注意力不动了。

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

阅读全文

相关推荐

最新文章