I. Differences between click events and touch events:
Click Event: For a button, the system responds to both the button and the touch screen event;
Touch event: For a button, only the touch screen event is returned;
Therefore, we recommend that you use the click event to process the button control.
Ii. Click Event Processing Template
Import Android. View. View. onclicklistener;
Public class XXXX activityextends Activity
{
Private onclicklistener otl_conn = (onclicklistener) New clicklistenerconn ();
Private button bv = NULL;
Protected void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. xxxx );
Bv = (button) findviewbyid (R. Id. xxxxbutton );
BV. setonclicklistener (otl_conn );
}
Class clicklistenerconn implements onclicklistener
{
Public void onclick (view v ){
Xxxxxx ......
}
}
}
Layout/xxxx. xml
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
Android: Orientation = "vertical" Android: Background = "# fff">
<Button Android: Id = "@ + ID/connbutton"
Android: text = "Connect"
Android: textcolor = "#000"
Android: textsize = "20sp"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</Linearlayout>
3. Touch event processing Template
Import Android. View. View. ontouchlistener;
Public class XXXX activityextends Activity
{
Private ontouchlistener otl_conn = (ontouchlistener) New touchlistenerconn ();
Private button bv = NULL;
Protected void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. xxxx );
Bv = (button) findviewbyid (R. Id. xxxxbutton );
BV. setontouchlistener (otl_conn );
}
Class touchlistenerconn implements ontouchlistener
{
Public Boolean ontouch (view V, motionevent event ){
Switch (event. getaction ()){
Case motionevent. action_down:
XXXX ....;
Break;
}
Return true;
}
}
}
Layout/xxxx. xml
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
Android: Orientation = "vertical" Android: Background = "# fff">
<Button Android: Id = "@ + ID/connbutton"
Android: text = "Connect"
Android: textcolor = "#000"
Android: textsize = "20sp"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</Linearlayout>
4. Click Event Processing another simplified Template
Import Android. View. View. onclicklistener;
Public class XXXX activityextends activity implements view. onclicklistener
{
Protected void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. xxxx );
Findviewbyid (R. Id. connbutton). setonclicklistener (this );
}
Public void onclick (view v ){
Switch (V. GETID ()){
Case R. Id. connbutton:
Xxxxxx ......
Break;
}
}
}
Reprinted please indicate the source: http://blog.csdn.net/ccwwff/archive/2010/08/18/5821419.aspx