Dynamic add controls for Android development

Source: Internet
Author: User

To add TextView controls dynamically:

One: Create an Android Project project

Activity_main.xml file:

1, with two linearlayout layout respectively wrapped a pair of textview,edittext control, set orientation to horizontal direction, edittext hint properties can achieve the watermark effect, Two edittext are used to control the display (number of TextView controls) of rows and columns.

2. Use a linearlayout layout to wrap button buttons, and when the EditText control is entered, the control is automatically generated when the button is clicked.

3. Use a tablelayout layout to display the generated controls in tabular form.

<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= "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= "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= "Generate table"/> </LinearLayout> </LinearLayout> <tablelayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/table1" > </TableLayout>

Mainactivity.java file:

1. WC and MP variables are used to set the width height of the auto-generated TextView control respectively.

2. First create two EditText, a button, a tablelayout variable, and get the corresponding ID of the control by Findviewbyid (R).

3, button through Setonclicklistener (New onclicklistener) {}); method Add listener event, implement onclick () Click button to trigger event.

4, two input width by integer.parseint (Row.gettext (). ToString ()) method gets the input and converts it to an integer type, GetText () gets the input value.

5. Code creation Control: Creates a control through the new control (mainactivity.this), Mainactity is an anonymous class.

 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) {if(Row.gettext (). Length () >0&&column.gettext (). Length () >0){                    //Converting input rows and columns to shaping                    intRow_int=Integer.parseint (Row.gettext (). toString ()); intCol_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                     for(inti=1;i<=row_int;i++) {TableRow TableRow=NewTableRow (mainactivity. This);  for(intj=1;j<=col_int;j++)                            {                                //TV is used to displayTextView tv=NewTextView (mainactivity. This); //button Bt=new button (mainactivity.this);Tv.settext ("(" +i+ "," +j+ ")");                            Tablerow.addview (TV); }                            //new TableRow added to TablelayoutTablelayout.addview (TableRow,NewTablelayout.layoutparams (MP, wc,1)); }                    }Else{toast.maketext (mainactivity). This, "Please enter rows and columns", 1). Show ();                    }            }        }); }  }

6, double cycle, the outermost layer is used to create the number of TableRow, which is used to create the number of columns.

7, Tablelayout table layout can be Removeallviews () method to clear the tabular data, to prevent the click two times duplicate content. All columns are automatically filled with blanks by setstretchallcolumns (true).

8. The TableRow in the Tablelayout table layout is equivalent to the TR label of table, and the TR can be appended to the table by AddView (), and the control can be appended to the row (TableRow) by this method.

Dynamic add controls for Android development

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.