有人可以解释后裔焦点=后裔吗? [英] Can someone explain descendantFocusability = afterDescendants?
问题描述
我很难理解descendantFocusability
.非常特别afterDescendants
.
I'm having hard time understanding descendantFocusability
. Very particulary afterDescendants
.
有人可以告诉我一个什么时候有用的例子吗?
Could someone show me an example of when this would be useful?
推荐答案
定义ViewGroup
及其后代之间的关系,以寻找View
作为焦点.
Defines the relationship between the ViewGroup
and its descendants when looking for a View
to take focus.
必须是以下常量值之一.
Must be one of the following constant values.
+------------------------------------------------------------------------------------------+
| Constant Value Description |
+------------------------------------------------------------------------------------------+
| afterDescendants 1 The ViewGroup will get focus only if |
| none of its descendants want it. |
+------------------------------------------------------------------------------------------+
| beforeDescendants 0 The ViewGroup will get focus before |
| any of its descendants. |
+------------------------------------------------------------------------------------------+
| blocksDescendants 2 The ViewGroup will block its descendants from |
| receiving focus. |
+------------------------------------------------------------------------------------------+
You can check the complete example here.
代码段是:
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
ListView listView = getListView();
Log.d(TAG, "onItemSelected gave us " + view.toString());
Button b = (Button) view.findViewById(R.id.button);
EditText et = (EditText) view.findViewById(R.id.editor);
if (b != null || et != null) {
// Use afterDescendants to keep ListView from getting focus
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
if(et!=null) et.requestFocus();
else if(b!=null) b.requestFocus();
} else {
if (!listView.isFocused()) {
// Use beforeDescendants so that previous selections don't re-take focus
listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
listView.requestFocus();
}
}
}
按照上面的代码片段,afterDescendants
用于防止listview
获得焦点,因此EditText
或Button
都可以请求焦点.
As per the above snippet, afterDescendants
is used to prevent listview
from getting focus, so that either EditText
or Button
can request focus.
注意:上面提供的链接已断开.请参阅我的要点以获取代码
Note: The link provided above is broken. Please refer to my Gist for the code
这篇关于有人可以解释后裔焦点=后裔吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!