Android View Add Listener instance code _android

Source: Internet
Author: User

Copy Code code as follows:

Findviewbyid (R.id.mybutton). Setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) {
Do stuff
}
});


The disadvantage of adding listener with the above method is that if there are too many controls, the number of listener will increase, so you can use the following tips to reduce the number of listener:

Copy Code code as follows:

View.onclicklistener handler = View.onclicklistener () {
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.ID.BUTTON01://Dostuff
Break
Case R.ID.BUTTON02://Dostuff
Break
}
}
}

Findviewbyid (R.id.mybutton). Setonclicklistener (handler);
Findviewbyid (R.id.myotherbutton). Setonclicklistener (handler);

In Android1.6, the work of adding listener is quite simple (feeling more like doing web programming!). ), the steps are as follows:

1. First define the button inside the layout and specify the response listener

Copy Code code as follows:

<xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"
/>
<button
android:text= "Button01"
Android:id= "@+id/button01"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:onclick= "MyClickHandler01"
/>
<button
android:text= "Button02"
Android:id= "@+id/button02"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:onclick= "MyClickHandler02"
/>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"
/>
</LinearLayout>

The following two lines are the new features:

android:onclick= "MyClickHandler01"

android:onclick= "MyClickHandler02"

2. Define public methods in the activity MyClickHandler01, and MYCLICKHANDLER02 (note that the two methods must have a view parameter).

Copy Code code as follows:

Package com.ray.test;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;

public class Testonclicklistener extends activity {

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);


}
public void MyClickHandler01 (View target) {
Settitle ("MyClickHandler01");
}
public void MyClickHandler02 (View target) {
Settitle ("MyClickHandler02");
}
}

Of course, you can also use this type of wording:

Set two buttons to the same listener

android:onclick= "Myclickhandler"

android:onclick= "Myclickhandler"

Copy Code code as follows:

Package com.ray.test;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;

public class Testonclicklistener extends activity {

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}
public void Myclickhandler (View target) {
Switch (Target.getid ()) {
Case R.ID.BUTTON01:
Settitle ("MyClickHandler01");
Break
Case R.ID.BUTTON02:
Settitle ("MyClickHandler02");
Break
}
}
}

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.