// 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 ".