android-Developing a custom component

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.