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