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

时间:2018-02-23 23:40:18

标签: android button onclicklistener control-flow

背景

我按照Pragnesh Ghota's solution of one onClick listener for every button的格式跟踪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
        );
    }

此外,我通过在actvity类中实现View.OnClickListener来制作one onClick listener。我的课程定义如下:

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

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

问题

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

问题

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

3 个答案:

答案 0 :(得分:1)

在创建每个按钮时,只需为每个按钮创建一个新的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
    );
}

答案 1 :(得分:0)

您可以为按钮设置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){
        .....
   }
 }
}

答案 2 :(得分:0)

最简单的解决方案是使用setTaggetTag作为按钮。您可以将对象与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;
    ...
  }
}