简体   繁体   中英

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

In my xml layout file I have 81 buttons like this:

<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(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.

I think this is what you want. You have to name the buttons as button01,button02,button03,etc.

 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

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:

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

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:

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.

To work with your buttons, just add the following to your Activity class, and do whatever you want:

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.

I hope you get the idea!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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