it-swarm-id.com

Dapatkan semua tampilan anak di dalam LinearLayout sekaligus

Saya memiliki LinearLayout, yang berisi beberapa TextViews anak. Bagaimana saya bisa mendapatkan pandangan anak tentang LinerLayout menggunakan loop?

126
Adham

Gunakan getChildCount() dan getChildAt(int index).

Contoh:

LinearLayout ll = …
final int childCount = ll.getChildCount();
for (int i = 0; i < childCount; i++) {
      View v = ll.getChildAt(i);
      // Do something with v.
      // …
}
263
Yashwanth Kumar
   ( (ViewGroup) findViewById(Android.R.id.content));// you can use this in an Activity to get your layout root view, then pass it to findAllEdittexts() method below.

Di sini saya hanya mengulangi EdiTexts, jika Anda menginginkan semua Tampilan, Anda dapat mengganti EditText dengan View.

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);
        }
    }

}
37
Favas Kv

gunakan ini

    final int childCount = mainL.getChildCount();
    for (int i = 0; i < childCount; i++) {
          View element = mainL.getChildAt(i);

        // EditText
        if (element instanceof EditText) {
            EditText editText = (EditText)element;
            System.out.println("ELEMENTS EditText getId=>"+editText.getId()+ " getTag=>"+element.getTag()+
            " getText=>"+editText.getText());
        }

        // CheckBox
        if (element instanceof CheckBox) {
            CheckBox checkBox = (CheckBox)element;
            System.out.println("ELEMENTS CheckBox getId=>"+checkBox.getId()+ " getTag=>"+checkBox.getTag()+
            " getText=>"+checkBox.getText()+" isChecked=>"+checkBox.isChecked());
        }

        // DatePicker
        if (element instanceof DatePicker) {
            DatePicker datePicker = (DatePicker)element;
            System.out.println("ELEMENTS DatePicker getId=>"+datePicker.getId()+ " getTag=>"+datePicker.getTag()+
            " getDayOfMonth=>"+datePicker.getDayOfMonth());
        }

        // Spinner
        if (element instanceof Spinner) {
            Spinner spinner = (Spinner)element;
            System.out.println("ELEMENTS Spinner getId=>"+spinner.getId()+ " getTag=>"+spinner.getTag()+
            " getSelectedItemId=>"+spinner.getSelectedItemId()+
            " getSelectedItemPosition=>"+spinner.getSelectedItemPosition()+
            " getTag(key)=>"+spinner.getTag(spinner.getSelectedItemPosition()));
        }

    }
3

Dapatkan semua tampilan pemandangan dan anak-anaknya secara rekursif di Kotlin:

private fun View.getAllViews(): List<View> {
    if (this !is ViewGroup || childCount == 0) return listOf(this)

    return children
            .toList()
            .flatMap { it.getAllViews() }
            .plus(this as View)
}
0
Stanislav Kinzl

Lebih mudah dengan Kotlin:

for (child in ll.children) {
     //child is a View         
}

Di sini ll adalah id dari LinearLayout didefinisikan dalam XML.

0
Malwinder Singh