访问特定布局的按钮(accessing the buttons of a specific layo

2019-10-29 07:10发布

我有一个答案: 每一个布局的按钮访问有关如何显示特定布局的所有的按钮,但我不明白这里的代码是我如何努力,使其运行该项目的其余部分excatly一样上述这里的链接是我变了:

package com.example.adapterlist;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {
    Map<String, Integer> layoutIds = new HashMap<String, Integer>();
    ArrayList<String> arrayForArrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ListView listView = (ListView) findViewById(R.id.mylist);
        java.lang.reflect.Field[] ID_Fields = R.layout.class.getFields();
        String []values=new String[ID_Fields.length];
        int[] resArray = new int[ID_Fields.length];
        for(int i = 0; i < ID_Fields.length; i++){
            try {
                resArray[i] = ID_Fields[i].getInt(null);
                values[i]=getResources().getResourceEntryName(resArray[i]);
                Log.v("resArray[i]  " , getResources().getResourceEntryName(resArray[i]));

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
             android.R.layout.simple_list_item_1, android.R.id.text1, values);
             listView.setAdapter(adapter);
             listView.setOnItemClickListener(new OnItemClickListener(){
             @Override
             public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    iJustClickedAnItemInTheList((String) listView.getSelectedItem());



                }
             });


    }

    private void iJustClickedAnItemInTheList(String idname) {
        setContentView(layoutIds.get(idname));
        ArrayList<Button> allButtonsInLayout = getViewsFromViewGroup(findViewById(android.R.id.content), Button.class);
    }

    public static <T> ArrayList<T> getViewsFromViewGroup(View root, Class<T> clazz) {
        ArrayList<T> result = new ArrayList<T>();
        for (View view : getAllViewsFromRoots(root)) 
            if (clazz.isInstance(view)) 
                result.add(clazz.cast(view));
        return result;
    }

    public static ArrayList<View> getAllViewsFromRoots(View...roots) {
        ArrayList<View> result = new ArrayList<View>();
        for (View root : roots)
            getAllViews(result, root);
        return result;
    }

    private static void getAllViews(ArrayList<View> allviews, View parent) {
        allviews.add(parent);
        if (parent instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup)parent;
            for (int i = 0; i < viewGroup.getChildCount(); i++)
                getAllViews(allviews, viewGroup.getChildAt(i));
        }
      }


    }

Answer 1:

这个怎么样? 唯一的一点是,你需要一个布局( /res/layout/listactivity.xml )与列表视图id为listView

public class MainActivity extends Activity {
    ListView lv;
    String[] layoutNames;
    int[] layoutIds;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listactivity);
        lv = (ListView)findViewById(R.id.listView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        fill();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, layoutNames);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                iJustClickedAnItemInTheList(arg2);
            }
        });
    }

    void fill() {
        java.lang.reflect.Field[] layoutFields = R.layout.class.getFields();
        layoutNames = new String[layoutFields.length];
        layoutIds = new int[layoutFields.length];
        for(int i = 0; i < layoutFields.length; i++){
            try {
                layoutIds[i] = layoutFields[i].getInt(null);
                layoutNames[i] = getResources().getResourceEntryName(layoutIds[i]);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    private void iJustClickedAnItemInTheList(int position) {
        setContentView(layoutIds[position]);
        ArrayList<Button> allButtonsInLayout = getViewsFromViewGroup(findViewById(android.R.id.content), Button.class);
        Toast.makeText(this, "" + allButtonsInLayout.size(), Toast.LENGTH_SHORT).show();
    }

    public static <T> ArrayList<T> getViewsFromViewGroup(View root, Class<T> clazz) {
        ArrayList<T> result = new ArrayList<T>();
        for (View view : getAllViewsFromRoots(root)) 
            if (clazz.isInstance(view)) 
                result.add(clazz.cast(view));
        return result;
    }

    public static ArrayList<View> getAllViewsFromRoots(View...roots) {
        ArrayList<View> result = new ArrayList<View>();
        for (View root : roots)
            getAllViews(result, root);
        return result;
    }

    private static void getAllViews(ArrayList<View> allviews, View parent) {
        allviews.add(parent);
        if (parent instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup)parent;
            for (int i = 0; i < viewGroup.getChildCount(); i++)
                getAllViews(allviews, viewGroup.getChildAt(i));
        }
    }
}


文章来源: accessing the buttons of a specific layout