問題
問題1:實際開發中,遇到了一個問題,發送驗證碼後,進入60s倒計時,因爲頁面過長,所以就用了ScrollView。結果,每次刷新倒計時值的時候,都會導致自動滾動。
問題2:ScrollView內部有EditText,當該頁面跳轉到其他頁面後,再回來的時候,焦點發生變化,導致了自動滾動。
網上的錯誤方案
1,在xml中加入下面的代碼
android:focusable="true"
android:focusableInTouchMode="tru
缺點:只能解決部分機型
2,在xml中加入下面代碼
android:descendantFocusability="blocksDescendants"
缺點:會屏蔽頁面上所有的輸入時間,EditText無法獲取焦點,不能輸入
正確做法
scrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
scrollView.setFocusable(true);
scrollView.setFocusableInTouchMode(true);
scrollView.setOnTouchListener((v, event) -> {
v.requestFocusFromTouch();
return false;
});