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
可以請求焦點。