Dynamically creating Buttons and setting onClickListener

后端 未结 9 969
囚心锁ツ
囚心锁ツ 2020-11-29 22:45

I have problem with handling dynamically created Buttons on Android. I\'m creating N buttons and I have to do the same method when button is clicked but I have to know which

相关标签:
9条回答
  • 2020-11-29 22:57
    public class MainActivity extends Activity implements View.OnClickListener
    {
    
    LinearLayout linearLayout;
    Button [] button;
    View.OnClickListener listener;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linearLayout=(LinearLayout)findViewById(R.id.parent_lay);
        String[] array={"U123","U124","U125"};
        int length=array.length;
        System.out.println("11111111111111111111111111");
        button=new Button[length];
        for(int i=0;i<length;i++)
        {
            button[i]=new Button(getApplicationContext());
            button[i].setId(i);
            button[i].setText("User" + i);
            button[i].setOnClickListener(this);
            linearLayout.addView(button[i]);
        }
    }
    @Override
    public void onClick(View view)
    {
        view.getId();
        Button button=(Button)findViewById(view.getId());
        button.setText("Changed");
    }
    }
    
    0 讨论(0)
  • 2020-11-29 23:00

    I got one solution for this.. use this code in onCreate

    linear = (LinearLayout) findViewById(R.id.linear);
    
    LayoutParams param = new LinearLayout.LayoutParams(
    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
    
    Button[] btn = new Button[num_array_name.length];
    for (int i = 0; i < num_array_name.length; i++) {
        btn[i] = new Button(getApplicationContext());
        btn[i].setText(num_array_name[i].toString());
        btn[i].setTextColor(Color.parseColor("#000000"));
        btn[i].setTextSize(20);
        btn[i].setHeight(100);
        btn[i].setLayoutParams(param);
        btn[i].setPadding(15, 5, 15, 5);
        linear.addView(btn[i]);
    
        btn[i].setOnClickListener(handleOnClick(btn[i]));
    
    }
    

    after onCreate create one method of return type View.OnClickListener like this..

    View.OnClickListener handleOnClick(final Button button) {
        return new View.OnClickListener() {
            public void onClick(View v) {
            }
        };
    }
    
    0 讨论(0)
  • 2020-11-29 23:06

    You could create a method that returns an onclickListener and takes a button as a parameter. And then use that method to set the onClicklistener in the first loop you have..

    Update: code could be soemthing along these lines:

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

    as a method in the activity and then use it in the loop like this

    button.setOnClickListener(getOnClickDoSomething(button));
    
    0 讨论(0)
  • 2020-11-29 23:06

    View IDs should not be used for these purposes as View Ids are generated on compilation time depending on IDs defined in xml layout files.

    Just place your own IDs in the setTag() method which is available at the View level (so Buttons inherit them). This "tag" can be anything that allow you to recognize a View from others. You retrieve its value with getTag().

    0 讨论(0)
  • 2020-11-29 23:07
    Button.OnClickListener btnclick = new Button.OnClickListener(){
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
    
            Button button = (Button)v;
        Toast.makeText(getApplicationContext(), button.getText().toString(),2).show();  
        }
    
    };
    

    call this listener by btn.setOnClickListener(btnclick);

    0 讨论(0)
  • 2020-11-29 23:13

    instead use setTag() function to distinct easily.

     for(int i=0;i<4;i++) {
         Button btn = new Button(this);
         btn.setTag(i);
         btn.setOnClickListener(new View.OnclickListener() {
             @Override
             public void onClick(View v) {
                 int i=v.getTag();
                 switch(i) {
                     case 1: btn.setText(i);
                         break;
    
                     case 2: btn.setText(i);
                         break;
    
                     case 3: btn.setText(i);
                         break;
    
                     case 4: btn.setText(i);
                         break;
    
                     default: btn.setText("Others");
                 }
             }
         }
    
    0 讨论(0)
提交回复
热议问题