Tôi đang gặp khó khăn trong việc tìm hiểu descendantFocusability
. Rất đặc biệt afterDescendants
.Ai đó có thể giải thích descendantFocusability = afterDescendants?
Ai đó có thể cho tôi xem ví dụ về thời điểm điều này hữu ích không?
Tôi đang gặp khó khăn trong việc tìm hiểu descendantFocusability
. Rất đặc biệt afterDescendants
.Ai đó có thể giải thích descendantFocusability = afterDescendants?
Ai đó có thể cho tôi xem ví dụ về thời điểm điều này hữu ích không?
Xác định mối quan hệ giữa ViewGroup
và hậu duệ của nó khi tìm kiếm View
để lấy nét.
Phải là một trong các giá trị hằng số sau.
+------------------------------------------------------------------------------------------+ | 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. | +------------------------------------------------------------------------------------------+
Bạn có thể kiểm tra ví dụ hoàn chỉnh here.
Đoạn là:
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();
}
}
}
Theo đoạn trên, afterDescendants
được sử dụng để ngăn chặn listview
từ việc tập trung, vì vậy mà một trong hai EditText
hoặc Button
có thể yêu cầu tập trung.