We learned the dynamic generation of tables, not just to dynamically build controls, but also to generate an event.
Next, use a little chestnut to learn about this point of knowledge.
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical" > <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Vertical" > <LinearLayout android:layout_weight= "1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <TextView android:layout_width= "Wrap_content"android:gravity= "Center_vertical"Android:layout_height= "Match_parent"Android:layout_marginleft= "10DP"android:textsize= "20SP"Android:text= "Please enter line:"/> <EditText Android:id= "@+id/edittext1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:ems= "10"Android:hint= "Please enter a number!" "Android:numeric= "decimal"/> </LinearLayout> <LinearLayout android:layout_weight= "1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <TextView android:layout_width= "Wrap_content"android:gravity= "Center_vertical"Android:layout_height= "Match_parent"Android:layout_marginleft= "10DP"android:textsize= "20SP"Android:text= "Please enter column:"/> <EditText Android:id= "@+id/edittext2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:ems= "10"Android:hint= "Please enter a number!" "Android:numeric= "decimal" > <requestfocus/> </EditText> </linearlayout > <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <Button android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/button1"Android:text= "One-click Auto-Generate Table"/> </LinearLayout> </LinearLayout> <tablelayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/table1" > </TableLayout></LinearLayout>
Packagecom.example.dynamictable;Importandroid.app.Activity;ImportAndroid.graphics.Color;ImportAndroid.os.Bundle;ImportAndroid.util.Log;Importandroid.view.Gravity;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.ViewGroup;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;Importandroid.widget.TableLayout;ImportAndroid.widget.TableRow;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {Private Final intWC =ViewGroup.LayoutParams.WRAP_CONTENT; Private Final intMP =ViewGroup.LayoutParams.MATCH_PARENT; PrivateEditText Row; PrivateEditText column; PrivateButton bt1; PrivateTablelayout tablelayout; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //Get Control buttonbt1=(Button) Findviewbyid (R.id.button1); //Get text input box controlrow=(EditText) Findviewbyid (R.ID.EDITTEXT1); Column=(EditText) Findviewbyid (R.ID.EDITTEXT2); //to bind the button buttons to an order-click eventBt1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//color[] colorblocks= new color[4096]; //int Row_int=integer.parseint (Row.gettext (). toString ()); //int Col_int=integer.parseint (Column.gettext (). toString ()); //Get Control TablelayoutTablelayout =(tablelayout) Findviewbyid (R.ID.TABLE1); //Clear All Rows of a tabletablelayout.removeallviews (); //all columns automatically fill in the blanksTablelayout.setstretchallcolumns (true); //generate a table of x rows, y columns intThelength = 64; for(inti=0;i<thelength;i++) {TableRow TableRow=NewTableRow (mainactivity. This); //Create color for(intj=0;j<thelength;j++) { intresult = I*thelength + (j+1)-1;//0~4095 intRed = result/256 ; intGreen = (result-red*256)/16; intBlue = result-red*256-green*16; //TV is used to displayTextView tv=NewTextView (mainactivity. This); //button Bt=new button (mainactivity.this);Tv.settext ("-"); Tv.setbackgroundcolor (Color.rgb (Red*16, green*16, blue*16) ); //Color.rgb (red*16-1, Green*16-1, blue*16-1)//Tv.setbackgroundresource (35434);Tablerow.addview (TV); } //new TableRow added to TablelayoutTablelayout.addview (TableRow,NewTablelayout.layoutparams (MP, wc,1)); } } }); } }
Operation Result:
Note that running time will be slow, when you always see that page does not move, do not be excited, please wait patiently, sometime. You know.
Due to the computer factor we do not cycle the full color out, only to take part of it. See the color table above, just part of it,
If you want to test your machine well, try 255 to make the entire color table
Believe that you will want to crash, because your computer or mobile phone is very difficult to hold.
Android_demo Create color layouts