동적으로 생성한 view들을 조작하고 싶은데, 해당 view들에는 id가 할당되어 있지 않아 난감한 적이 있었다. 그 때 찾은 방법이 있는데 자세한 코드와 내용은 링크를 참고하자.
1
2
3
4
5
6
7
LinearLayout ll = …
final int childCount = ll.getChildCount();
for (int i = 0; i < childCount; i++) {
View v = ll.getChildAt(i);
// Do something with v.
// …
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SparseArray<Edittext> array = new SparseArray<Edittext>();
private void findAllEdittexts(ViewGroup viewGroup) {
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++) {
View view = viewGroup.getChildAt(i);
if (view instanceof ViewGroup)
findAllEdittexts((ViewGroup) view);
else if (view instanceof Edittext) {
Edittext edittext = (Edittext) view;
array.put(edittext.getId(), edittext);
}
}
}
참고
android - Get all child views inside LinearLayout at once - Stack Overflow
'안드로이드' 카테고리의 다른 글
안드로이드 databinding setViewmodel 에러 (3) | 2018.10.19 |
---|---|
[링크] 안드로이드 fragment를 사용하는 이유 (1) | 2018.08.12 |
Nordic Android BLE Library 정리 (0) | 2018.07.24 |
안드로이드 커스텀 Logger (0) | 2018.07.17 |
Invoke-customs are only supported starting with Android O--min-api 26 (0) | 2018.07.15 |