問題描述
似乎是一個常見問題,但我沒有找到很好的解決方案.目標是阻止 ScrollView
自動滾動到具有焦點的 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.
ScrollView
中有一堆視圖(Button
s、TextView
s 等),其中一個是 >編輯文本
.單擊 ScrollView
中的按鈕后,ScrollView
會向下滾動到 EditText
(它的屏幕外).這是不希望的,因為您不希望將其他元素滾動到屏幕之外.
You have a bunch of views (Button
s, TextView
s, 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.
現在我可以通過在 ScrollView
中添加其他可聚焦元素來阻止在屏幕首次顯示時發生這種情況.但是,普遍的問題仍然存在.用戶手動向下滾動到 EditText
,輸入一些數字,然后向上滾動到頂部(EditText
現在離開屏幕),他們單擊 ScrollView 中的按鈕
,你猜怎么著?ScrollView
向下滾動到那個該死的 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
.
我正在考慮擴展 ScrollView
并覆蓋其中的一些方法,例如 findFocusableViewInBounds
,但我覺得我只會讓自己陷入更多麻煩.
I'm thinking about extending the ScrollView
and overriding some of the methods there like findFocusableViewInBounds
, but I have a feeling I'll just be getting myself into more trouble.
如果可以,請提供幫助.
Please help if you can.
我嘗試過在 ScrollView
頂部設置 0 高度 EditText
,將 Next Focusable 元素屬性添加到 ScrollView
等.我想一個hack"可能是讓 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
設置為Before Descendants",focusable
設置為true"并且 focusableInTouchMode
設置為true".這將不是 ScrollView
本身,而是您擁有各種視圖的內部布局.接下來添加一個 onTouchListener
到您的 ScrollView
中,當它被觸摸時,它會從 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.
這篇關于停止 ScrollView 自動滾動到 EditText的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!