android-Developing a custom component
One custom component classification
1 Customized Component:
Current components are imperfect, often inheriting view, adding more properties and events, vertical expansion
2 Compound Component:
Inherit ViewGroup, put multiple simple controls through the layout into a conforming control, horizontal expansion
two defining component steps
1 Select Inheritance:
Sub-class for view or view
Initialization of Class 2
New properties and initial value settings
3 Overloaded methods
Layout and presentation
OnDraw (), Onmessure ()
4 event mechanism
Onkeyevent
Listener
Overloaded functions (Viewer mode)
5 Custom Events
Establishing an interface
Interface Oniconbuttonlistener
{
public void OnKeyDown (String pkeyobject);
}
Building Interface Objects
Private Oniconbuttonlistener Moniconbuttonlistener;
Setting Event listener functions
public void Setoniconbuttonlistener (Oniconbuttonlistener poniconbuttonlistener)
{
Poniconbuttonlistener = Moniconbuttonlistener;
}
Overriding methods that callback certain events
@Override
public boolean onKeyDown (int keycode, keyevent event)
{
TODO auto-generated Method Stub
if (Moniconbuttonlistener! = null)
{
Moniconbuttonlistener.onkeydown ("Hello Chengzhi");
}
Return Super.onkeydown (KeyCode, event);
}
Three use Components
In the XML file
If the component class is not an internal class
< package name + Class Name properties >
<com.chengzhi.standerviewhomework.myiconbutton Property/>
If the component class is an inner class
<view Package Name $ class Name property > When the component is an internal class for activity
<view class= "Com.chengzhi.standerviewhomework.myiconbuttonactivity$myiconbutton" Property/>
android-Developing a custom component