ANDROID Auto generate dynamic table for

Source: Internet
Author: User
Tags gettext

Simple chestnut to understand this automatically generated dynamic control (automatically generated table)

/cs-layout/res/layout/activity_main.xml

<LinearLayoutxmlns: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" >    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "vertical" >        <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1"android:orientation= "Horizontal" >            <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:layout_marginleft= "10DP"android:gravity= "Center_vertical"Android:text= "Please enter line:"android:textsize= "20SP" />            <EditTextAndroid:id= "@+id/edittext1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:ems= "Ten"Android:hint= "Please enter a number!" "Android:numeric= "decimal" />        </LinearLayout>        <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1"android:orientation= "Horizontal" >            <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:layout_marginleft= "10DP"android:gravity= "Center_vertical"Android:text= "Please enter column:"android:textsize= "20SP" />            <EditTextAndroid:id= "@+id/edittext2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:ems= "Ten"Android:hint= "Please enter a number!" "Android:numeric= "decimal" >                <Requestfocus/>            </EditText>        </LinearLayout>        <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" >            <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "auto-Generate table with one click" />        </LinearLayout>    </LinearLayout>    <TablelayoutAndroid:id= "@+id/flout"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" >    </Tablelayout></LinearLayout>
Activity_main.xml

/cs-layout/src/com/example/cs_layout/mainactivity.java

 Packagecom.example.cs_layout;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 classMainactivityextendsActivity {PrivateButton submit; PrivateEditText Cloumn; PrivateEditText Run; PrivateTablelayout flout; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //cloumn =;Submit =(Button) Findviewbyid (R.id.button1); Cloumn=(EditText) Findviewbyid (R.ID.EDITTEXT1); Run=(EditText) Findviewbyid (R.ID.EDITTEXT2); Flout=(tablelayout) Findviewbyid (r.id.flout); Submit.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {flout.removeallviews (); NewMainactivity (). Shows (Cloumn, run, flout, mainactivity. This);    }        }); }     Public voidshows (EditText Cloumn, EditText Run, tablelayout flout, Activity a) {intc = Integer.parseint (Cloumn.gettext () + "");//converts the incoming number to an integer        intR = Integer.parseint (Run.gettext () + ""); Tablelayout Table=NewTablelayout (a);//Table LayoutTable.setweightsum (1);//Weights//table.setcolumnstretchable (0, false);         for(inti = 0; I < C; i++) {TableRow tr=NewTableRow (a);  for(intj = 0; J < R; J + +) {Button b=NewButton (a); //B.settext (j);Tr.addview (b);        } table.addview (TR);    } flout.addview (table); }     Public voidcolor () {}}
Mainactivity.java

ANDROID Auto generate dynamic table for

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.