Android Auto Generate table

Source: Internet
Author: User

Layout.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical" > <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_weight= "6" > <tablelayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:layout_gravity= "Center_vertical" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Please enter the row number column to generate the table"android:textsize= "15sp"/> <TableRow android:gravity= "Center"Android:layout_margintop= "5DP" > <TextView Android:id= "@+id/tv1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "line"android:textsize= "15sp"/> <EditText Android:id= "@+id/et1"Android:layout_width= "100DP"Android:layout_height= "Wrap_content"/> </TableRow> <TableRow android:gravity= "Center"Android:layout_margintop= "5DP" > <TextView Android:id= "@+id/tv2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "column"android:textsize= "15sp"/> <EditText Android:id= "@+id/et2"Android:layout_width= "100DP"Android:layout_height= "Wrap_content"Android:numeric= "decimal"/> <!--decimal can only enter a number, greater than 0 of the number--</TableRow> <TableRow android:gravity= "Center" > <Button Android:id= "@+id/btnadd"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_span= "2"Android:text= "OK"/> </TableRow> </TableLayout> </linea Rlayout> <tablelayout Android:id= "@+id/table"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_weight= "3"Android:stretchcolumns="*"Android:shrinkcolumns= "*" > </TableLayout></LinearLayout>

Activity.java

  

Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;Importandroid.widget.TableLayout;ImportAndroid.widget.TableRow; Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateButton submit; PrivateEditText Row; PrivateEditText Cloumn; Privatetablelayout table; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.canchange); Submit=(Button) Findviewbyid (R.id.btnadd); Row=(EditText) Findviewbyid (R.ID.ET1); Cloumn=(EditText) Findviewbyid (R.id.et2); Table=(tablelayout) Findviewbyid (r.id.table); Submit.setonclicklistener ( This); } @Override Public voidOnClick (View v) {intc = Integer.parseint (Row.gettext () + ""); intR = Integer.parseint (Cloumn.gettext () + "");        Table.removeallviews ();  for(inti = 0; I < C; i++) {TableRow tr=NewTableRow ( This);  for(intj = 0; J < R; J + +) {Button b=NewButton ( This);            Tr.addview (b);        } table.addview (TR); }    }}

Effect:

    

 When you don't know Lingyunmu, you should be straight to the beginning of the road high

Android Auto Generate table

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.