Composite Control
If you do not want to create a fully customized component, but want to put a group of existing controls together to form a reusable component, then create a composite component (or composite control) is a suitable choice. In a container, a composite component combines multiple atomic controls (or views) into a logical group that can handle a single task. For example, the ComboBox control is composed of a single line text field, a button, and a pop-up list. If you press the button and select an item from the list, the selection item will be filled in a single text field. If you like it, you can also directly input the content to the text field.
In Android, there are actually two other views that can easily work on ComboBox: spinner and autocompletetextview. However, the example of using ComboBox makes the concept easier to understand.
To create a composite component, follow these steps:
1. Generally, the starting point is to create a class with a certain type of layout. In the ComboBox example, you can use the horizontal linearlayout layout. Other la s can also be nested in the interior so that composite components can be freely combined. Note: Like activity, you can use XML Declaration-based methods to create a composite component or program them into code.
2. In the constructor of the new class, you can bring the parameters required by any subclass, and pass them through the constructor of the subclass first. Then, you can create other views used in the new component. The ComboBox component creates a text field and a pop-up list at this time. Note: You can also introduce your own attributes and parameters to the XML Declaration. These attributes and parameters can be used by the constructor.
3.
You can also create a listener for the events that may be generated by the included view. For example, for the click listener method of the list item, if a list item is selected, the listener method updates the content of the text field.
4. create custom attributes with accessors and editors, for example, get... And set... Method.
5. When extending a layout, the ondraw and onmeasure () methods do not need to be rewritten, because the default layout behavior can work well. However, you can rewrite them as needed.
6. You can also rewrite other on... Method. For example, you can override the onkeydown () method. When a key is pressed, you can select some default values from the ComboBox pop-up list.
To sum up, using la s as the basis for custom controls has the following benefits:
1.
The layout can be specified using declared XML files like activity, and views can be created programmatically and embedded into the layout from code.
2. The ondraw () and onmeasure () methods (and most other on... Methods), so you do not need to rewrite them.
3. Finally, you can quickly construct complex composite views of tasks and reuse them as a single component.
Examples of composite controls
In the sdk api demos project, there are two list examples-Example 4 and Example 6 in the views/lists directory demonstrate a speechview component, it expands the lineralayout layout so that the component displays the voice query results. The corresponding classes are in the sample code of list4.java and list6.java.
Modify the existing view type
In some cases, it is even easier to create custom views. If there is an existing component that is very similar to what you want, you can simply extend it and rewrite the behavior you want to change. You can use fully-customized components to do everything you want, but by starting with a more special class in the view hierarchy tree, you can also get more actions that you want to achieve.
For example, in the notepad application examples included in the SDK, there are many features that use the Android platform, including a single-line notepad that extends the edittext control. This is not a perfect example, but it demonstrates some principles of component customization.
Use the following link to import the notepad sample code to eclipse. Let's take a look at the linededittext class definition in the noteeditor. Java file.
Http://developer.android.com/resources/samples/NotePad/index.html
Notes:
1. Definition
Use the following method to define a class:
Public static class linededittextextends edittext
A. It is defined as an internal class of noteediteffecti.pdf, but it is public. If necessary, it can be accessed outside the noteeditor class using noteeditor. myedittext.
B. it is static, which means that no synthesis method is generated to allow access to data from the parent class, which in turn means that it is actually an independent class, there is no mandatory association with noteeditor. If you do not need to access the status from outside the class, you must keep generating a small class and allow easy use of it in other classes, this is a clear method for creating internal classes.
C. It extends the edittext class, which is the custom view we choose. After customization, the new class can replace the normal edittext class.
2.
Class initialization
The first thing to call is the super method. It is not the default constructor, but a parameterized method. Edittext is created using these parameters in the XML layout file. Therefore, our constructor needs to include these parameters and pass them to the constructor of the parent class.
3. Rewrite Method
In this example, only one method is rewritten: ondraw ().
For the notepad example, the override ondraw () method allows us to draw a blue line on the edittext canvas (the canvas is input by the override ondraw () method ). At the end of this method, the super. ondraw () method is called. The method of the parent class should be called, but in this example, we call the ondraw () method of the parent class after drawing the desired line.
4. Use custom components
How can I use a custom component? In the notepad example, custom components are directly used in the declared layout. Therefore, you need to take a look at the Declaration in the note_editor.xml file in the Res/layout file:
<View xmlns: Android = "http://schemas.android.com/apk/res/android"
Class = "com. example. Android. notepad. noteeditor $ linededittext"
Android: Id = "@ + ID/Note"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Background = "@ Android: color/transparent"
Android: padding = "5dp"
Android: scrollbars = "vertical"
Android: fadingedge = "vertical"
Android: gravity = "TOP"
Android: textsize = "22sp"
Android: Capitalize = "sentences"/>
Custom components are created using a general view in XML, and classes are specified using the complete package name. Note the method for referencing internal classes in the definition: noteeditor $ linededittext, which is a standard method for referencing internal classes in the Java programming language. If the custom view component is not defined as an internal class, you must use the XML element name to declare the view component and remove the class attributes, such:
<Com. Android. notepad. linededittext
Id = "@ + ID/Note"
.../>
Note that the linededittext class is an independent class file. This technology will not work when this class is nested in the noteeditor class.
Other attributes and parameters in the definition are passed into the constructor of the custom component and then passed to the constructor of the edittext class. Therefore, these attributes and parameters are the same as those used by the edittext class. Note: You can also add your own parameters.