Code for adding a Listener instance to Android View

Source: Internet
Author: User

Copy codeThe Code is as follows: findViewById (R. id. myButton). setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
// Do stuff
}
});

The disadvantage of using the above method to add Listener is that if there are too many controls, the number of Listener will increase. Therefore, you can use the following tips to reduce the number of Listener:

Copy codeThe Code is 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. mytherbutton). setOnClickListener (handler );

In Android1.6, adding Listener is quite simple (it feels more like programming on Web pages !), The procedure is as follows:

1. Define the Button in layout and specify the Listener of the response.

Copy codeThe Code is 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 new features:

Android: onClick = "myClickHandler01"

Android: onClick = "myClickHandler02"

2. Define the public methods myClickHandler01 and myClickHandler02 in the activity (note that the two methods must have a View parameter ).

Copy codeThe Code is 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 the following method:

Set the two buttons to the same Listener.

Android: onClick = "myClickHandler"

Android: onClick = "myClickHandler"

Copy codeThe Code is 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;
}
}
}

Related Article

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.