Implementation method of sliding switch for developing advanced custom controls for Android [with demo source code download]
This paper describes the implementation method of sliding switch for Android to develop advanced user-defined controls. Share with you for your reference, as follows:
Custom switch control
There are generally three ways to customize Android controls
1. Inherit the inherent controls of Android and add functions and logic on the basis of Android native controls.
2. Inheriting ViewGroup, this kind of custom control can add other child controls to its own layout.
3. Inheriting view, this kind of custom control does not have too many similarities with the native control, nor does it need to add other child controls in its own belly.
The toggleview custom switch control is not similar to the Android native control in characterization, and does not follow the Android native control in sliding effect, so our custom toggleview chooses to inherit the view
The same custom control needs to duplicate three construction methods
Because it is a custom control, it is better to define the property yourself. We define three properties here
1. Background picture 2. Picture of slider 3. Status of default switch in layout
So you need to use custom attributes
In the values directory, create a new XML file, attrs xml
Define your own attributes in it
< declare styleable name attribute > is the attribute name that can be found in the R file
In < attr > tags, an attribute name is written for each tag, the attribute represents the attribute name, and the format represents the attribute type
Three attribute names and attribute types are defined here.
After the three construction methods of property and custom control have been completed, we can add custom controls to the layout file
Note: in my custom control com hss. toggle. In toggleview, some attributes start with Android and some attributes start with HSS (my own defined namespace). Why?
Look at the second line of the code,
I write a line of code here to show that values / attrs After each entry in XML is imported, you can directly use my attrs Attributes in XML
After you can directly use the custom attribute, the question should focus on how to get the value of my custom attribute in Java code?
Get according to the namespace and the name value of the custom attribute. See the code:
See? This method uses the attr parameter, so the operation of obtaining the user-defined attribute value should be performed in the one of the two parameters.
See the code for the overall custom control class:
At this point, the logic of our custom control part is written, and then we call it in MainActivity.
Toastutil class is as follows:
To sum up, the steps of customizing the control are as follows:
1. In values / attrs XML custom attribute and data type of attribute value
2. Define custom control classes in Java code, inherit view or ViewGroup or Android native controls, implement construction methods, obtain the values of custom properties, and write corresponding logic and click events.
3. Use custom controls and custom attributes in layout files (note namespaces).
4. Call in MainActivity
Click here to download the complete example code.
More readers interested in Android related content can view the special topics of this site: summary of Android control usage, summary of Android view skills, summary of Android SQLite database skills, summary of Android JSON format data skills, summary of Android database operating skills, summary of Android file operating skills Summary of SD card operation methods for Android programming development, introduction and advanced tutorial for Android development, and summary of Android resource operation skills
I hope this article will help you in Android programming.