New controls in Android activity

Source: Internet
Author: User

Knowing the 5 layouts, we'll find that these layouts are static, how do you get the system to automatically generate controls? That would require activity to help.

Today we're talking about creating a new layout with activity.

Use the case to say it!

Implement an input row and column to automatically generate a table and create a background color

Effect

    

The code is as follows:

  

<?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= "Wrap_content"android:orientation= "Vertical" > <EditText Android:id= "@+id/text_row"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:layout_gravity= "Left|top"Android:ems= "10"Android:hint= "@string/text_row"Android:inputtype= "number" > <requestfocus/> </EditText> <EditText Android:id= "@+id/text_column"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:layout_gravity= "Left|top"Android:ems= "10"Android:hint= "@string/text_column"Android:inputtype= "Number"/> <Button Android:id= "@+id/btn_commit"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_vertical|center_horizontal"Android:text= "@string/btn_commit"/> </LinearLayout> <tablelayout android:layout_weight= "1"Android:id= "@+id/tablelayout"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > </TableLayout></LinearLayout>

1  Packagecom.example.linearlayout;2 3 Importandroid.app.Activity;4 ImportAndroid.graphics.Color;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.Menu;7 ImportAndroid.view.MenuItem;8 ImportAndroid.view.View;9 ImportAndroid.view.View.OnClickListener;Ten ImportAndroid.widget.Button; One Importandroid.widget.LinearLayout; A Importandroid.widget.TableLayout; - ImportAndroid.widget.TableRow; - ImportAndroid.widget.TextView; the ImportAndroid.widget.Toast; -  -  -  Public classMainactivityextendsActivity { +     PrivateTextView Text_column; -     PrivateTextView Text_row; +     Privatetablelayout Tablay; A     Private intLength = 64; at @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (R.LAYOUT.LAYOUT2); -Get Control button -Button Btn_commit =(Button) Findviewbyid (r.id.btn_commit); in        
Get Control TextView -Text_column =(TextView) Findviewbyid (r.id.text_column); totext_row=(TextView) Findviewbyid (r.id.text_row); + -Set a click event for a button theBtn_commit.setonclicklistener (NewOnclicklistener () { * $ @OverridePanax Notoginseng Public voidOnClick (View v) { -   

Get text value theString column =Text_column.gettext (). toString (). Trim (); +String row =Text_row.gettext (). toString (). Trim (); ADetermines whether the empty the if(Column.length ()! = 0 && row.length ()! = 0){ +Get Control layout -tablay=(tablelayout) Findviewbyid (r.id.tablelayout); $ $Clear first. - tablay.removeallviewsinlayout (); -Tablay.setstretchallcolumns (true); theStrong Turn integral type - intX =integer.parseint (Text_column.gettext (). toString ());//??Wuyi inty =Integer.parseint (Text_row.gettext (). toString ()); the - WuFor loop nesting - for(intm = 0; M < y; m++ ){ About $TableRow TR =NewTableRow (mainactivity. This); - - for(intn = 0; n < x; n++ ){ - ATextView TV =NewTextView (mainactivity. This); + the //Tv.settext ("(" +n+ "," +m+ ")"); -Tv.settext (""); $ Tr.addview (TV); theGet parameters, set background color values the intresult = M*n +2*n-1; the intRed = RESULT/16/16 ; the intGreen = (result-red*16*16)/16; - intBlue = result-red*16*16-green*16; in theTv.setbackgroundcolor (Color.rgb (red*16, green*16, blue*16)); the About the } the Tablay.addview (TR); the + } - the}Else{BayiToast.maketext (mainactivity. This, "Please enter rows and columns", 1). Show (); the } the } - }); - the } the}

    

New controls in Android activity

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.