OnClickListener() 用于动态数量的按钮

OnClickListener() for dynamic amount of buttons

背景

我按照 Pragnesh Ghota's solution of one onClick listener for every button in the format of dymmeh's individual initialization solution:

在 for 循环中动态创建按钮
LinearLayout someLayout = (LinearLayout) findViewById(R.id.theRoom);
    for (int i = 0; i < neededButtons.length; i++){
        neededButtons[i] = new Button(this);
        neededButtons[i].setText(names[i]);
        neededButtons[i].setOnClickListener(this);
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
    }

此外,我通过在活动 class 中实施 View.OnClickListener 来制作 one onClick listener。我的 class 是这样定义的:

public class RecallActivity extends AppCompatActivity implements View.OnClickListener{
    ...
}

我已成功完成 Pragnesh Ghota's solution 的其他步骤。然而...

问题

Pragnesh Ghota's solution的第四步提到使用case语句检查是否有任何按钮被点击。这在按钮数量已知时有效。但是,由于我遵循 dymmeh 的解决方案 中规定的格式,在执行之前我不知道要检查多少个按钮。

问题

如何在覆盖的 onClickMethod 中为动态数量的按钮执行控制流语句?

只需在创建按钮时为每个按钮创建一个新的 OnClickListener。

LinearLayout someLayout = (LinearLayout) findViewById(R.id.theRoom);
for (int i = 0; i < neededButtons.length; i++){
    neededButtons[i] = new Button(this);
    neededButtons[i].setText(names[i]);
    neededButtons[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // add your click listener code here
                    }
                })
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
    );
}

你可以为按钮设置一个id。就像这样:

LinearLayout someLayout = (LinearLayout) findViewById(R.id.theRoom);
for (int i = 0; i < neededButtons.length; i++){
    neededButtons[i] = new Button(this);
    neededButtons[i].setText(names[i]);
    neededButtons[i].setId(i);
    neededButtons[i].setOnClickListener(this);
    ...
    ); 
}

然后在 OnClickListener 中按 ID 查找视图。例如:

public class RecallActivity extends AppCompatActivity implements View.OnClickListener{
  @overide
  public void onClick(View view){
     if(view.getId == 0){
        .....
   }
 }
}

最简单的解决方案是为您的按钮使用 setTag and getTag。您可以使用具有 setTag 和 getTag 的对象。每当您创建按钮时,为其设置标签:

for (int i = 0; i < neededButtons.length; i++){
    neededButtons[i] = new Button(this);
    neededButtons[i].setText(names[i]);
    neededButtons[i].setTag(names[i]);
    // or you can use the index as the tag with:
    // neededButtons[i].setTag(i);
    neededButtons[i].setOnClickListener(this);
}

然后你通过检查标签为每个按钮做一些事情:

@Override
public void onClick(View v) {
    doSomething(v.getTag());
}

private void doSomething(Object tag) {
  // in case your tag is the index, than you can convert it to 
  // integer and use switch case
  int index = (int) tag;
  switch(index) {
    case 1:
      ...
      break;
    case 2:
      ...
      break;
    ...
  }
}