繁体   English   中英

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

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

在我的xml布局文件中,我有81个按钮,如下所示:

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

我知道我可以像这样获得参考:

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

等等,但是我如何有效地做到这一点呢?

编辑:

我想用对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);}} 

但是我不知道如何将button11更改为button12,依此类推。

我想这就是你想要的。 您必须将按钮命名为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();
                }


              }} 

您是否考虑过列表视图或网格视图? 如果您可以使用其中任一方法,则效率会更高。 如果不是,则必须一个一个地声明每个按钮,但是如果使用playButtons数组,则可以使用循环将更改应用于所有按钮,例如

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

这将是我想到的最有效的方法...

我建议您对ID使用此语法,这样您就不会对应用程序中的ID过多感到困惑:

android:id="@+id/your_file_name_widgetName_whatYourWidgetDo

在名为menu_settings.xml的布局文件中保存设置预设的按钮的示例

android:id="@+id/menu_settings_button_savePreset"

当我的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

    }

}

您告诉您布局上有81个按钮, for循环中的findViewById()会在低性能循环中产生,建议您将android:onClick = "methodName" attr添加到xml文件中的按钮上,因此不要每个都需要使用findViewById()

要使用按钮,只需将以下内容添加到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

    }

    ...

}

但是请考虑一下:您的布局确实需要这么多按钮吗? 想想在更好的应用程序体系结构中,单个线程的81个按钮可能非常繁重。

希望您能理解!

暂无
暂无

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

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