【问题标题】:OnClickListener() for dynamic amount of buttonsOnClickListener() 用于动态数量的按钮
【发布时间】:2018-02-23 23:40:18
【问题描述】:

背景

我正在通过Pragnesh Ghota's solution of one onClick listener for every buttondymmeh'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
        );
    }

此外,我通过在活动类中实现 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 中为动态数量的按钮执行控制流语句?

【问题讨论】:

    标签: android button onclicklistener control-flow


    【解决方案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
        );
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        最简单的解决方案是为您的按钮使用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;
            ...
          }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-26
          • 2018-09-07
          • 2020-03-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多