Android: dynamically create multiple buttons and add listening events to each button

Source: Internet
Author: User

// Obtain the screen size to properly set the button size and position

Displaymetrics dm = new displaymetrics ();
Getwindowmanager (). getdefaultdisplay (). getmetrics (DM );
Int width = DM. widthpixels;
Int Height = DM. heightpixels;

// Customize the layout component
Relativelayout layout = new relativelayout (this );

// Create 16 buttons here, with four buttons in each line

Button BTN [] = new button [16];
Int J =-1;
For (INT I = 0; I <= 15; I ++ ){
BTN [I] = new button (this );
BTN [I]. setid (2000 + I );
BTN [I]. settext ("button" + I );
Relativelayout. layoutparams btparams = new relativelayout. layoutparams (width-50)/); // you can specify the width and height of a button.
If (I % 4 = 0 ){
J ++;
}
Btparams. leftmargin = 10 + (width-50)/4 + 10) * (I % 4); // locate the X coordinate
Btparams. topmargin = 20 + 55 * j; // ordinate position
Layout. addview (BTN [I], btparams); // Add the button to the layout component.
}
This. setcontentview (layout );
// Configure listeners in batches

For (int K = 0; k <= BTN. Length-1; k ++ ){
// No findid is required here, because the ID corresponding to the button has been determined during creation.
BTN [K]. settag (k); // set a tag for the button to confirm which button is pressed

BTN [K]. setonclicklistener (New button. onclicklistener (){
@ Override
Public void onclick (view v ){
Int I = (integer) v. gettag ();

Intent intent = new intent ();
Intent. setclass (work_01.this, work_02.class );
Bundle bundle = new bundle ();
Bundle. putint ("count", I );
Intent. putextras (bundle );
Startactivity (intent );
Work_01.this.finish ();
}
});
}

Then, you can print the "press button:" I in work_02 to confirm whether the listener is set correctly.

For example, press the button 0. In the corresponding page of work_02, the "pressed button is 0" should be printed ".

// Obtain the screen size to properly set the button size and position

Displaymetrics dm = new displaymetrics ();
Getwindowmanager (). getdefaultdisplay (). getmetrics (DM );
Int width = DM. widthpixels;
Int Height = DM. heightpixels;

// Customize the layout component
Relativelayout layout = new relativelayout (this );

// Create 16 buttons here, with four buttons in each line

Button BTN [] = new button [16];
Int J =-1;
For (INT I = 0; I <= 15; I ++ ){
BTN [I] = new button (this );
BTN [I]. setid (2000 + I );
BTN [I]. settext ("button" + I );
Relativelayout. layoutparams btparams = new relativelayout. layoutparams (width-50)/); // you can specify the width and height of a button.
If (I % 4 = 0 ){
J ++;
}
Btparams. leftmargin = 10 + (width-50)/4 + 10) * (I % 4); // locate the X coordinate
Btparams. topmargin = 20 + 55 * j; // ordinate position
Layout. addview (BTN [I], btparams); // Add the button to the layout component.
}
This. setcontentview (layout );
// Configure listeners in batches

For (int K = 0; k <= BTN. Length-1; k ++ ){
// No findid is required here, because the ID corresponding to the button has been determined during creation.
BTN [K]. settag (k); // set a tag for the button to confirm which button is pressed

BTN [K]. setonclicklistener (New button. onclicklistener (){
@ Override
Public void onclick (view v ){
Int I = (integer) v. gettag ();

Intent intent = new intent ();
Intent. setclass (work_01.this, work_02.class );
Bundle bundle = new bundle ();
Bundle. putint ("count", I );
Intent. putextras (bundle );
Startactivity (intent );
Work_01.this.finish ();
}
});
}

Then, you can print the "press button:" I in work_02 to confirm whether the listener is set correctly.

For example, press the button 0. In the corresponding page of work_02, the "pressed button is 0" should be printed ".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.