有人能解释一下“后代”吗?

我很难理解 descendantFocusability。非常特别的 afterDescendants

谁能给我举个例子说明一下这个什么时候会有用?

39279 次浏览

定义在寻找 View以获得焦点时 ViewGroup及其子代之间的关系。

必须是下列常数值之一。

+------------------------------------------------------------------------------------------+
|      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.                                   |
+------------------------------------------------------------------------------------------+

您可以检查完整的示例 给你

片段如下:

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获得焦点,这样 EditTextButton都可以请求焦点。

注意 : 上面提供的链接已中断。请参阅我的 要点了解代码