Selection of static and dynamic components

Source: Internet
Author: User

1. Concept:

The view component can be created statically, even if the XML file is created with code.

Dynamic is generally loaded with code; Static is generally loaded with XML

2. How to use:

"1". Create a view using XML--recommended!!

Components on the Android graphical user interface can be created using XML files

An XML file that uses attributes to specify the properties of a component, such as an ID.

The XML file is placed under Res/layout

"2". Create a view with code--sometimes it makes use of

Each view component is an object of view type

You can create a view object in your code by using the construction method of the Views class

You can invoke the Setxxx method of the View object to set its properties

3. Examples of dynamic use:

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"Tools:context=". Mainactivity " >    <ButtonAndroid:id= "@+id/showdatedialog"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Show Date dialog box" />    <ButtonAndroid:id= "@+id/showtimedialog"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Show time dialog box" /></LinearLayout>

Code:

 Public classMainactivityextendsActivity {PrivateButton Showdatedialog; PrivateButton Showtimedialog; Private intYear ; Private intmonthofyear; Private intDayOfMonth; Private intHourofday; Private intminute; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Showdatedialog=(Button) Findviewbyid (R.id.showdatedialog); Showtimedialog=(Button) Findviewbyid (R.id.showtimedialog); //Get system TimeCalendar Calendar=calendar.getinstance (); year=Calendar.get (calendar.year); Monthofyear=Calendar.get (Calendar.month); DayOfMonth=Calendar.get (Calendar.day_of_month); LOG.D ("H_BL", Year + "-" + Monthofyear + "-" +dayofmonth); Hourofday=Calendar.get (Calendar.hour_of_day); Minute=Calendar.get (Calendar.minute); Showdatedialog.setonclicklistener (NewViewocl ()); Showtimedialog.setonclicklistener (NewViewocl ()); }    /*** Click on the event Listener in the popup dialog box * *@authorAdministrator **/    Private classViewoclImplementsView.onclicklistener {@Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.showdatedialog://Date dialog BoxDatepickerdialog Datedialog=NewDatepickerdialog (mainactivity. This,Newdatesetcls (), year, Monthofyear, DayOfMonth);                Datedialog.show ();  Break;  CaseR.id.showtimedialog://Time dialog BoxTimepickerdialog Timedialog=NewTimepickerdialog (mainactivity. This,NewOntimesetlistener () {@Override Public voidOntimeset (timepicker view,intHourofday,intminute) {Toast.maketext (Getapplicationcontext (),"Select time:" + Hourofday + "-" +minute, Toast.length_long). Show (); }}, Hourofday, minute ,true);                Timedialog.show ();  Break; }        }    }    Private classDatesetclsImplementsOndatesetlistener {@Override Public voidOndateset (DatePicker view,intYearintMonthofyear,intdayofmonth) {Toast.maketext (Getapplicationcontext (),"Select Date:" + year + "-" + (Monthofyear + 1) + "-" +DayOfMonth, Toast.length_long). Show (); }    }}

Selection of static and dynamic components

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.