A composite widget is an atomic and reusable widget that contains multiple child controls and is associated in a certain layout.
When creating a composite control, you need to define the layout, appearance, and the interaction between the views it contains. The composite control is created by extending a viewgroup. To create a composite control, you need to select a layout class that best fits the child control to expand it, as shown in the following framework code:
Public class mycompoundview extends linearlayout {
Public mycompoundview (context ){
Super (context );
}
Public mycompoundview (context, attributeset attrs ){
Super (context, attrs );
}
}
To be used with the activity, the first choice for creating the UI for the composite control is to use the layout resource. The following code snippet shows the XML definition of layout. layout defines a simple widget consisting of an edittext and a button. The button is responsible for clearing the content:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Edittext
Android: Id = "@ + ID/edittext"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
<Button
Android: Id = "@ + ID/clearbutton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "clear"
/>
</Linearlayout>
To use layout of a new widget, you must rewrite its constructor and use the inflate method of the layoutinflate System Service to expand the layout resource. The inflate method carries the parameters of the layout resource and returns an expanded view. In this case, the returned view should be the class you are creating, so you need to input a parent view and set to automatically append the result to the parent view. The following code is used.
The following code shows the clearableedittext class. In the constructor, it expands the layout resource created above and obtains references to the control contained in it. In addition, the hookupbutton method is also called, which is used to clean up text when the button is pressed.
Public class clearableedittext extends linearlayout {
Edittext;
Button clearbutton;
Public clearableedittext (context ){
Super (context );
// Inflate the view from the layout resource.
String infservice = context. layout_inflater_service;
Layoutinflater Li;
Li = (layoutinflater) getcontext (). getsystemservice (infservice );
Li. Inflate (R. layout. clearable_edit_text, this, true );
// Get references to the child controls.
Edittext = (edittext) findviewbyid (R. Id. edittext );
Clearbutton = (button) findviewbyid (R. Id. clearbutton );
// Hook up the functionality
Hookupbutton ();
}
}
If you like to build layout in code, you can implement it just as you do for activity. The following code snippet shows how to override the clearableedittext constructor to create the same UI as in XML:
Public clearableedittext (context ){
Super (context );
// Set orientation of layout to vertical
Setorientation (linearlayout. Vertical );
// Create the child controls.
Edittext = new edittext (getcontext ());
Clearbutton = new button (getcontext ());
Clearbutton. settext ("clear ");
// Lay them out in the compound control.
Int lheight = layoutparams. wrap_content;
Int lwidth = layoutparams. fill_parent;
Addview (edittext, new linearlayout. layoutparams (lwidth, lheight ));
Addview (clearbutton, new linearlayout. layoutparams (lwidth, lheight ));
// Hook up the functionality
Hookupbutton ();
}
Once the screen has been built, you can connect to the event manager for each sub-control to provide the functions you need. In the following snippet, The hookupbutton method fills in the Code for clearing text when the button is pressed:
Private void hookupbutton (){
Clearbutton. setonclicklistener (New button. onclicklistener ()
{
Public void onclick (view V)
{
Edittext. settext ("");
}
});
}
Sample Code:
Http://files.cnblogs.com/xirihanlin/DL090722@cc-CompoundControl.zip