8
我很难理解descendantFocusability
。特别是afterDescendants
。有人可以解释descendantFocusability = afterDescendants吗?
有人可以告诉我什么时候这会有用吗?
我很难理解descendantFocusability
。特别是afterDescendants
。有人可以解释descendantFocusability = afterDescendants吗?
有人可以告诉我什么时候这会有用吗?
定义ViewGroup
及其后代之间的关系,当寻找View
来关注。
必须是以下常数值之一。
+------------------------------------------------------------------------------------------+ | 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. | +------------------------------------------------------------------------------------------+
您可以查看完整的示例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
可以请求焦点。