Остановить ScrollView от установки фокуса на EditText
Android ScrollView (при прокрутке или fling'd) любит устанавливать фокус EditText, когда он является одним из его детей. Это происходит, когда вы прокручиваете и затем отпускаете. Есть ли способ остановить это поведение? Я пробовал почти все, о чем я могу думать, и все, что я читал в StackOverflow. Ничто не сработало для меня. Ниже приведен пример макета, если вы хотите его протестировать. Я отчаянно пытаюсь остановить это немое поведение.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginRight="50dp"
android:focusable="true"
android:focusableInTouchMode="true"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="TestApp 1"
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TestApp 1"
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TestApp 1"
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TestApp 1"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="Bleh...."
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TestApp 1"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="TestApp 1"
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TestApp 1"
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="MeeEEEEEEEEEEEEE"
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TestApp 1"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Ответы
Ответ 1
Это работает. Не уверен, что это лучшее решение или нет.
// SCROLL VIEW HACK
// BOGUS
ScrollView view = (ScrollView)findViewById(R.id.scrollView);
view.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.requestFocusFromTouch();
return false;
}
});
Ответ 2
Во-первых, вы можете удалить родительский элемент ScrollView.
Во-вторых, добавьте параметры фокусировки для дочернего элемента (только один ребенок на ScrollView):
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
Вот как выглядит ваш xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="50dp"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<EditText
android:id="@+id/editText_one"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="TestApp 1" />
<Button
android:id="@+id/btn_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TestApp 1" />
<Button
android:id="@+id/btn_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TestApp 1" />
<Button
android:id="@+id/btn_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TestApp 1" />
<EditText
android:id="@+id/editText_two"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="Bleh...." />
<Button
android:id="@+id/btn_four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TestApp 1" />
<EditText
android:id="@+id/editText_three"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="TestApp 1" />
<Button
android:id="@+id/btn_five"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TestApp 1" />
<Button
android:id="@+id/btn_six"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MeeEEEEEEEEEEEEE" />
<Button
android:id="@+id/btn_seven"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TestApp 1" />
</LinearLayout>
</ScrollView>
Также обратите внимание, что у вас есть несколько идентификаторов с тем же именем. Попробуйте дать им уникальные имена.
Если вы хотите только скрыть мягкую клавиатуру, и пусть EditText все еще имеет фокус, вы можете сделать это, добавив следующее свойство вашей активности в манифест:
android:windowSoftInputMode="stateHidden"
Ответ 3
ах очень поздно. Но я все же хочу добавить это решение, чтобы помочь новичкам, таким как я.
ScrollView sv = (ScrollView)findViewById(R.id.scrollView);
sv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
sv.clearFocus();
return false;
}
});
да, это работает, просто установите прослушиватель в clearfocus. и это лучше, чем запрос requestFocusFromTouch в другое представление "x", что приведет к той же проблеме - прокрутку к представлению "x".