简体   繁体   English

如何获得对Android中大量按钮的引用?

[英]How do I get references to large amount of buttons in android?

In my xml layout file I have 81 buttons like this: 在我的xml布局文件中,我有81个按钮,如下所示:

<Button
    android:id="@+id/button11"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

I know that I can get reference like this: 我知道我可以像这样获得参考:

playButtons[0][0] = (Button) findViewById(R.id.button11);
playButtons[0][1] = (Button) findViewById(R.id.button12);

and so on, but how can I do it efficiently? 等等,但是我如何有效地做到这一点呢?

edit: 编辑:

I want to fill my playButtons matrix with references to XML buttons in for loop like this: 我想用对for循环中的XML按钮的引用填充我的playButtons矩阵,如下所示:

for(int i = 0; i<9; i++){ 
      for(int k = 0; k<9; k++){ 
            playButtons[i][k] = (Button) findViewById(R.id.button11);}} 

But I don't know how to change button11 to button12 and so on. 但是我不知道如何将button11更改为button12,依此类推。

I think this is what you want. 我想这就是你想要的。 You have to name the buttons as button01,button02,button03,etc. 您必须将按钮命名为button01,button02,button03等。

 Button[][] playButtons = null;
        Class id=R.id.class;
         Field field = null;
         for(int i = 0; i<9; i++){ 
              for(int k = 0; k<9; k++){ 
                  try {
                    field = id.getField("button"+i+""+k);

                playButtons[i][k] = (Button) findViewById(field.getInt(null));
                } catch (NoSuchFieldException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


              }} 

Have you considered a listview or gridview? 您是否考虑过列表视图或网格视图? If you could use either of those that would be much more efficient. 如果您可以使用其中任一方法,则效率会更高。 If not you are going to have to declare each button one by one but if you use the playButtons array you can use a loop to apply changes to all the buttons for example 如果不是,则必须一个一个地声明每个按钮,但是如果使用playButtons数组,则可以使用循环将更改应用于所有按钮,例如

for(int i = 0;i<playButtons.length;i++){
  playButtons[0][i].setOnClickListener(this);
 }

and this would be the most efficient way I could think of... 这将是我想到的最有效的方法...

I'll suggest you to use this syntax to your IDs, so you won't get confused with large amout of IDs on your app: 我建议您对ID使用此语法,这样您就不会对应用程序中的ID过多感到困惑:

android:id="@+id/your_file_name_widgetName_whatYourWidgetDo

Example of a button that saves a settings preset, inside a layout file called menu_settings.xml 在名为menu_settings.xml的布局文件中保存设置预设的按钮的示例

android:id="@+id/menu_settings_button_savePreset"

When I have a lot of buttons on my XML file, I register it on array, like this: 当我的XML文件上有很多按钮时,我将其注册在数组上,如下所示:

final int[] BUTTON_ID = { 

    R.id.myId, R.id.anotherId, R.id.otherId,
    R.id.moreId, R.id.anotherAnotherId

};

final int BUTTONS_COUNT = BUTTON_ID.lenght;

Button[] button;

@Override
public void onCreate(Bundle savedInstanceState){

    ...

    button = new Button[BUTTONS_COUNT];

    for(int i = 0; i < BUTTONS_COUNT; i++){

        button[i] = (Button) findViewById(BUTTONS_ID[i]);

        button[i].setOnClickListener(this); // your class should implements View.onClickListener to use "this" as argument

    }

}

You told you have 81 buttons on your layout, the findViewById() inside for loop will result on low perfomanced loop, I suggest you to add the android:onClick = "methodName" attr to your buttons on xml file, so you don't need to use findViewById() for each. 您告诉您布局上有81个按钮, for循环中的findViewById()会在低性能循环中产生,建议您将android:onClick = "methodName" attr添加到xml文件中的按钮上,因此不要每个都需要使用findViewById()

To work with your buttons, just add the following to your Activity class, and do whatever you want: 要使用按钮,只需将以下内容添加到Activity类中,然后执行任何您想做的事情:

public void methodName(View v){

    Button b = (Button) v;

    int ID = b.getId();


    if(ID == R.id.buttonId){

        //DO SOMETHING FOR THIS BUTTON

    }


    else if(ID == R.id.anotherButtonId){

        //DO SOMETHING FOR THIS BUTTON

    }

    ...

}

But think about it: you do need so many buttons on your layout? 但是请考虑一下:您的布局确实需要这么多按钮吗? Think in a better app architecture, 81 buttons can be very heavy to a single Thread. 想想在更好的应用程序体系结构中,单个线程的81个按钮可能非常繁重。

I hope you get the idea! 希望您能理解!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM