Android Listener-based event handling

Source: Internet
Author: User

Android uses listeners to implement event handling in a number of ways:

The following section of the import package is omitted, please import it yourself when copying

    1. Activity itself as an event listener

Activity_main.xml

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"      xmlns:tools= "Http://schemas.android.com/tools"     android:layout_width= "Match_parent"     android:layout_height= "Match_parent"     android:paddingbottom= "@ Dimen/activity_vertical_margin "    android:paddingleft=" @dimen/activity_horizontal_margin "     android:paddingright= "@dimen/activity_horizontal_margin"      android:paddingtop= "@dimen/activity_vertical_margin"     tools:context= " Com.example.testevent.MainActivity " >    <TextView         android:id= "@+id/textview1"         android: Layout_width= "Wrap_content"         android:layout_height= "wrap_content "        android:text= "@string/hello_world"  />    <Button         android:id= "@+id/button1"         android:layout_width= " Wrap_content "        android:layout_height=" Wrap_content "         android:layout_alignleft= "@+id/textview1"          android:layout_below= "@+id/textview1"         android : text= "button"  /></RelativeLayout>

Mainactivity.java

public class Mainactivity extends Activity implements Onclicklistener{textview TextView; Button button, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); TextView = (TextView) Findviewbyid (r.id.textview1); button = (button) Findviewbyid (R.id.button1); Button.setonclicklistener (this);} @Overridepublic void OnClick (View v) {textview.settext ("I was clicked");}}

2. Anonymous inner class as event listener

XML file Ibid.

Mainactivity.java

public class Mainactivity extends Activity {button button; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); button = (Button) Findviewbyid ( R.id.button1); Button.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {TextView TextView = (TextView) Findviewbyid (r.id.textview1); Textview.settext ("I was clicked");}});}}

3. Defining an external class as an event listener

XML file Ibid.

Mainactivity.java

public class Mainactivity extends Activity {TextView TextView; Button button, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); TextView = (TextView) Findviewbyid (r.id.textview1); button = (button) Findviewbyid (R.id.button1); Button.setonlongclicklistener (New Textchangelistener (this, TextView));}}

Textchangelistener.java

public class Textchangelistener implements Onlongclicklistener {TextView textview;public textchangelistener (Activity AC, TextView TextView) {this.textview = TextView;} @Overridepublic boolean Onlongclick (View v) {textview.settext ("I've been clicked for a long time"); return true;}

4. Bind the event listener directly to the label

Activity_main.xml

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"      xmlns:tools= "Http://schemas.android.com/tools"     android:layout_width= "Match_parent"     android:layout_height= "Match_parent"     android:paddingbottom= "@ Dimen/activity_vertical_margin "    android:paddingleft=" @dimen/activity_horizontal_margin "     android:paddingright= "@dimen/activity_horizontal_margin"      android:paddingtop= "@dimen/activity_vertical_margin"     tools:context= " Com.example.testevent.MainActivity " >    <TextView         android:id= "@+id/textview1"         android: Layout_width= "Wrap_content"         android:layout_height= "wrap_content "        android:text= "@string/hello_world"  />    <Button         android:id= "@+id/button1"         android:layout_width= " Wrap_content "        android:layout_height=" Wrap_content "         android:layout_alignleft= "@+id/textview1"          android:layout_below= "@+id/textview1"         android : onclick= "Buttonclickhandle"         android:text= "button"  /> </RelativeLayout>

Mainactivity.java

public class Mainactivity extends Activity {TextView TextView; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} This must be publicpublic void Buttonclickhandle (View v) {TextView = (TextView) Findviewbyid (R.ID.TEXTVIEW1); Textview.settext ("I was clicked");}}


Android Listener-based event handling

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.