【问题标题】:onClickListener dynamically ANDROIDonClickListener 动态 ANDROID
【发布时间】:2014-02-28 19:21:30
【问题描述】:

我已经使用以下代码创建了动态按钮,我想听听它们。创建的按钮数量未定义,因为变量 drawView.getNumeroMallas() 是一个动态数字,并且每次都可以更改。 当我听它们时,我需要知道每个按钮的每个索引“i”。如何动态实现 onClickListener?谢谢。

         LinearLayout buttonsLayout = (LinearLayout)findViewById(R.id.linearlayoutUp);

        for(int i=0;i<drawView.getNumeroMallas();i++){

            Button buttonMalla = new Button(this);
            Button buttonRotar = new Button(this);
            buttonMalla.setText("Malla "+(i+1));
            buttonsLayout.addView(buttonMalla);
            buttonsLayout.addView(buttonRotar);
        }

【问题讨论】:

    标签: android button onclick


    【解决方案1】:

    为您动态创建的每个按钮分配一个单击侦听器,并将i 值作为 ID 分配给每个按钮,例如:

    buttonMalla.setOnClickListener(new myClickListener());
    buttonMalla.setId(i);
    

    您只需要检查在onClick() 中调用了哪个按钮。

    例如:

    class myClickListener implements OnClickListener {
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch(v.getId()) {
              case 1:
                  // button with ID==1 is clicked
                 break;
            }
        }
    

    【讨论】:

    • 感谢您的回答。我正在尝试,我注意到我的变量 drawView.getNumeroMallas() 也是动态创建的,因此每次按钮的数量都可能不同。因此我不知道如何实现 onclick,因为我不知道案例的数量。
    • 你为什么不早点发布这个细节?这完全是浪费时间!
    【解决方案2】:

    试试这个-

        LinearLayout buttonsLayout = (LinearLayout)findViewById(R.id.linearlayoutUp);
        for(int i=0;i<drawView.getNumeroMallas();i++){
    
            Button buttonMalla = new Button(this);
            Button buttonRotar = new Button(this);
            buttonMalla.setText("Malla "+(i+1));
            buttonMalla.setTag("M"+i);
            buttonRotar.setTag("R"+i);
            buttonMalla.setOnClickListner(dynamicButtonListner);
            buttonRotar.setOnClickListner(dynamicButtonListner);
            buttonsLayout.addView(buttonMalla);
            buttonsLayout.addView(buttonRotar);
        }
    
    //outside of any method write this - 
    private OnClickListner dynamicButtonListner = 
    new OnClickListner(){
         onClick(View v)
                {
                  String identifier = (String)v.getTag();
                  if(identifier.chartAt(0)=='M')
                  {
                     // malla button
                     int i = Integer.parseInt(identifier.chartAt(1));
                  }
                  else
                  {
                   //rotor button
                   int i = Integer.parseInt(identifier.chartAt(1));
                  }
                }      };
    

    【讨论】:

      【解决方案3】:

      通过这种方式,你有两个按钮的onClickListener,但如果你只想要一个按钮的onClickListener,删除另一个,

       for(int i=0;i<drawView.getNumeroMallas();i++){
      
                  Button buttonMalla = new Button(this);
                  Button buttonRotar = new Button(this);
                  buttonMalla.setText("Malla "+(i+1));
                  buttonMalla.setId(i+1);
                  buttonRotar.setText("Rotar "+(i+1));
                  buttonRotar.setId(i+1);
                 // buttonsLayout.addView(buttonMalla);
                  //buttonsLayout.addView(buttonRotar);
      
                  final int index = i+1;
      
                  buttonMalla.setOnClickListener(new OnClickListener() {
                     public void onClick(View v) {
                       Log.i("TAG1", "The index is" + index);
      
      
                          }
                      });
                      buttonsLayout.addView(buttonMalla);
      
                 buttonRotar.setOnClickListener(new OnClickListener() {
                      public void onClick(View v) {
                        Log.i("TAG2", "The index is" + index);
                                  }
                              });
                       buttonsLayout.addView(buttonRotar);
                  }
      
                  }
      

      【讨论】:

      • 听起来不错,我去试试
      【解决方案4】:

      试试这个:

      View.OnClickListener getOnClickDoSomething(final Button button)  {
          return new View.OnClickListener() {
              public void onClick(View v) {
                  button.setText("text now set.. ");    
              }
          };
      }
      

      【讨论】:

        【解决方案5】:

        在你的 for 循环中放置:

        buttonMalla.setOnClickListener(new OnCellClickListner(....what you need here as parameter of constructor));
        buttonRotar .setOnClickListener(new OnCellClickListner(....what you need here as parameter of constructor));
        

        oncellclicklistener 的类在这里:

        class OnCellClickListner implements OnClickListener {
        
        
        
         public OnCellClickListner(...){
                  //initialize here }
            public void onClick(View arg0) {
            //do your job here
        
            }} 
        

        【讨论】:

          猜你喜欢
          • 2012-04-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多