访问每个布局按钮 [英] accessing buttons of every layout

查看:183
本文介绍了访问每个布局按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的是访问单击项目(布局)的按钮;这code ++工程,但只显示所有的按钮,TextView的,......所有布局不点击之一。
如果你想运行此code可以用按钮添加其他布局测试
谢谢你的帮助
这是我的布局:

这是<强> activity_main.xml中

 &LT;?XML版本=1.0编码=UTF-8&GT?;
    &LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直&GT;        &LT; ListView控件
            机器人:ID =@ + ID / listView_button
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT&GT;
        &LT; /&的ListView GT;
       &LT;的TextView
          机器人:ID =@ + ID / myTextView
          机器人:layout_width =FILL_PARENT
         机器人:layout_height =25dp
         机器人:TEXTSIZE =23sp/&GT;
    &LT; / LinearLayout中&GT;

然后我MainActivity:

 包com.example.adapterlist;
进口的java.util.ArrayList;
进口的java.util.HashMap;
进口的java.util.Map;进口android.os.Bundle;
进口android.app.Activity;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.ListView;公共类MainActivity延伸活动{
    地图&LT;字符串,整数&GT; layoutIds =新的HashMap&LT;字符串,整数&GT;();
    ArrayList的&LT;串GT; arrayForArrayAdapter;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        最终的ListView的ListView =(ListView控件)findViewById(R.id.mylist);
        java.lang.reflect.Field中的[] ID_Fields = R.layout.class.getFields();
        的String []值=新的String [ID_Fields.length]
        INT [] = resArray新INT [ID_Fields.length]
        的for(int i = 0; I&LT; ID_Fields.length;我++){
            尝试{
                resArray [I] = ID_Fields [I] .getInt(NULL);
                值[I] = getResources()getResourceEntryName(resArray [I])。
                Log.v(resArray [I],getResources()getResourceEntryName(resArray [I]));            }赶上(抛出:IllegalArgumentException五){
                e.printStackTrace();
            }赶上(IllegalAccessException E){
                e.printStackTrace();
            }
        }
     ArrayAdapter&LT;串GT;适配器=新ArrayAdapter&LT;串GT;(这一点,
                android.R.layout.simple_list_item_1,android.R.id.text1,价值观);
             listView.setAdapter(适配器);
             listView.setOnItemClickListener(新OnItemClickListener(){                @覆盖
                公共无效onItemClick(适配器视图&LT;&GT;为arg0,ARG1查看,
                        INT ARG2,长ARG3){
                    iJustClickedAnItemInTheList((字符串)listView.getSelectedItem());
                }
             });
私人无效iJustClickedAnItemInTheList(字符串idname){
        的setContentView(layoutIds.get(idname));
        ArrayList的&LT;按钮和GT; allButtonsInLayout = getViewsFromViewGroup(findViewById(android.R.id.content),Button.class);
    }    公共静态&LT; T&GT; ArrayList的&LT; T&GT; getViewsFromViewGroup(查看根类&LT; T&GT; clazz所){
        ArrayList的&LT; T&GT;结果=新的ArrayList&LT; T&GT;();
        对于(查看视图:getAllViewsFromRoots(根))
            如果(clazz.isInstance(视图))
                result.add(clazz.cast(视图));
        返回结果;
    }    公共静态的ArrayList&LT;视图&gt; getAllViewsFromRoots(查看...根){
        ArrayList的&LT;视图&gt;结果=新的ArrayList&LT;视图&gt;();
        对于(查看根:根)
            getAllViews(结果,根);
        返回结果;
    }    私有静态无效getAllViews(ArrayList的&LT;视图&gt; allviews,查看父){
        allviews.add(父);
        如果(父的instanceof的ViewGroup){
            ViewGroup中的ViewGroup =(ViewGroup中)父母;
            的for(int i = 0; I&LT; viewGroup.getChildCount();我++)
                getAllViews(allviews,viewGroup.getChildAt(I));
        }
    }

最后,我的类MySimpleArrayAdapter:

 进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.TextView;公共类MySimpleArrayAdapter扩展ArrayAdapter&LT;串GT; {
  私人最终上下文的背景下;
  私人最终的String []值;  公共MySimpleArrayAdapter(上下文的背景下,的String []值){
    超(背景下,R.layout.troisieme,价值观);
    this.context =背景;
    this.values​​ =值;
  }  @覆盖
  公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    LayoutInflater吹气=(LayoutInflater)上下文
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    查看rowView = inflater.inflate(R.layout.troisieme,父母,假);
    TextView中的TextView =(TextView中)rowView.findViewById(R.id.myTextView);
    // ImageView的ImageView的=(ImageView的)rowView.findViewById(R.id.icon);
    textView.setText(值[位置]);
    //改变图标的​​Windows和iPhone
    字符串s =值[位置]
    返回rowView;
  }
}


解决方案

要得到所有的按钮从单一的布局:

 公共静态&LT; T&GT; ArrayList的&LT; T&GT; getViewsFromViewGroup(查看根类&LT; T&GT; clazz所){
    ArrayList的&LT; T&GT;结果=新的ArrayList&LT; T&GT;();
    对于(查看视图:getAllViewsFromRoots(根))
        如果(clazz.isInstance(视图))
            result.add(clazz.cast(视图));
    返回结果;
}公共静态的ArrayList&LT;视图&gt; getAllViewsFromRoots(查看...根){
    ArrayList的&LT;视图&gt;结果=新的ArrayList&LT;视图&gt;();
    对于(查看根:根)
        getAllViews(结果,根);
    返回结果;
}私有静态无效getAllViews(ArrayList的&LT;视图&gt; allviews,查看父){
    allviews.add(父);
    如果(父的instanceof的ViewGroup){
        ViewGroup中的ViewGroup =(ViewGroup中)父母;
        的for(int i = 0; I&LT; viewGroup.getChildCount();我++)
            getAllViews(allviews,viewGroup.getChildAt(I));
    }
}

MainActivity 你叫

 的ArrayList&LT;按钮和GT; allButtonsOnCurrentLayout = getViewsFromViewGroup(findViewById(android.R.id.content),Button.class);

现在你有一个的ArrayList 所有按钮从目前的布局。你可以遍历列表,并获取其ID与你已经显示的反射,或展示自己的标签,等等。

编辑:最后,我试图帮助,延长低于code用自己的

 公共类MainActivity延伸活动{
    地图&LT;字符串,整数&GT; layoutIds =新的HashMap&LT;字符串,整数&GT;();
    ArrayList的&LT;串GT; arrayForArrayAdapter;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        对于(场场:R.layout.class.getFields()){
            尝试{
                INT ID = field.getInt(NULL);
                字符串名称= getResources()getResourceEntryName(ID)。
                layoutIds.put(姓名,身份证);
                arrayForArrayAdapter.add(名);
            }
            赶上(抛出:IllegalArgumentException五){
                e.printStackTrace();
            }
            赶上(IllegalAccessException E){
                e.printStackTrace();
            }
        }
    }    私人无效iJustClickedAnItemInTheList(字符串idname){
        的setContentView(layoutIds.get(idname));
        ArrayList的&LT;按钮和GT; allButtonsInLayout = getViewsFromViewGroup(findViewById(android.R.id.content),Button.class);
    }    公共静态&LT; T&GT; ArrayList的&LT; T&GT; getViewsFromViewGroup(查看根类&LT; T&GT; clazz所){
        ArrayList的&LT; T&GT;结果=新的ArrayList&LT; T&GT;();
        对于(查看视图:getAllViewsFromRoots(根))
            如果(clazz.isInstance(视图))
                result.add(clazz.cast(视图));
        返回结果;
    }    公共静态的ArrayList&LT;视图&gt; getAllViewsFromRoots(查看...根){
        ArrayList的&LT;视图&gt;结果=新的ArrayList&LT;视图&gt;();
        对于(查看根:根)
            getAllViews(结果,根);
        返回结果;
    }    私有静态无效getAllViews(ArrayList的&LT;视图&gt; allviews,查看父){
        allviews.add(父);
        如果(父的instanceof的ViewGroup){
            ViewGroup中的ViewGroup =(ViewGroup中)父母;
            的for(int i = 0; I&LT; viewGroup.getChildCount();我++)
                getAllViews(allviews,viewGroup.getChildAt(I));
        }
    }
}

如果该好好尝试一下帮助你,我不知道会怎样。现在使用 allButtonsInLayout 显示按钮的id,或不管它是你想要的一切<$ C $办C>按钮。

what i need is to access the buttons of the clicked item(layout); this code works but just display all buttons,TextView,...of all layouts not the clicked one. if you want to run this code you can add other layouts with buttons for the test thank you for your help here is my layout:

This is activity_main.xml

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/listView_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
       <TextView        
          android:id="@+id/myTextView"    
          android:layout_width="fill_parent"    
         android:layout_height="25dp"    
         android:textSize="23sp" />
    </LinearLayout>

Then my MainActivity:

    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.View.OnClickListener;
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));
        }
    }

And eventually my class MySimpleArrayAdapter:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
  private final Context context;
  private final String[] values;

  public MySimpleArrayAdapter(Context context, String[] values) {
    super(context, R.layout.troisieme, values);
    this.context = context;
    this.values = values;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.troisieme, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.myTextView);
    //ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    textView.setText(values[position]);
    // Change the icon for Windows and iPhone
    String s = values[position];


    return rowView;
  }
} 

解决方案

To get all the Buttons from a single layout:

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

from your MainActivity you call

ArrayList<Button> allButtonsOnCurrentLayout = getViewsFromViewGroup(findViewById(android.R.id.content), Button.class);

Now you have an ArrayList with all Buttons from the current layout. You can iterate the list and fetch their ID's with the reflection you already displayed, or show their tags, et cetera.

Edit: My last attempt to help, extend the code below with your own

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);
        for(Field field : R.layout.class.getFields()){
            try {
                int id = field.getInt(null);
                String name = getResources().getResourceEntryName(id);
                layoutIds.put(name, id);
                arrayForArrayAdapter.add(name);
            } 
            catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

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

If this doens't help you, I do not know what will. Now use allButtonsInLayout to show the Button id's, or whatever it is you want to do with all the buttons.

这篇关于访问每个布局按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆