Get all child views inside LinearLayout at once

前端 未结 6 2064
野性不改
野性不改 2020-11-28 04:39

I have a LinearLayout, which contains several child TextViews. How can I get child views of that LinerLayout using a loop?

相关标签:
6条回答
  • 2020-11-28 05:25

    Get all views from any type of layout

    public List<View> getAllViews(ViewGroup layout){
            List<View> views = new ArrayList<>();
            for(int i =0; i< layout.getChildCount(); i++){
                views.add(layout.getChildAt(i));
            }
            return views;
    }
    

    Get all TextView from any type of layout

    public List<TextView> getAllTextViews(ViewGroup layout){
            List<TextView> views = new ArrayList<>();
            for(int i =0; i< layout.getChildCount(); i++){
                View v =layout.getChildAt(i);
                if(v instanceof TextView){
                    views.add((TextView)v);
                }
            }
            return views;
    }
    
    0 讨论(0)
  • 2020-11-28 05:28

    use this

        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()));
            }
    
        }
    
    0 讨论(0)
  • 2020-11-28 05:29

    Use getChildCount() and getChildAt(int index).

    Example:

    LinearLayout ll = …
    final int childCount = ll.getChildCount();
    for (int i = 0; i < childCount; i++) {
          View v = ll.getChildAt(i);
          // Do something with v.
          // …
    }
    
    0 讨论(0)
  • 2020-11-28 05:29

    It is easier with Kotlin using for-in loop:

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

    Here ll is id of LinearLayout defined in layout XML.

    0 讨论(0)
  • 2020-11-28 05:30

    Get all views of a view plus its children recursively in 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 讨论(0)
  • 2020-11-28 05:37
    ((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.
    

    Here I am iterating only EdiTexts, if you want all Views you can replace EditText with 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);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题