In the development for the control to add Listener is a very common work, the easiest way to add Listener can be:

1
  findViewById (R.id.myButton). setOnClickListener (new View.OnClickListener () (public void onClick (View v) (/ / Do stuff)));

Add a Listener using the above method has a drawback is that if control is too large, Listener will increase the number, so the following tips can be used to reduce the number of Listener:

1
  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);

The Android1.6 inside, add the Listener's work has become fairly simple (feeling more like doing web programming!), Concrete steps are as follows:

1. First, the definition in the layout inside the Button and specify the response Listener

1
  <? 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>

These two lines of which the following are new features:

android: onClick = "myClickHandler01"

android: onClick = "myClickHandler02"

2. In the event inside the definition of public methods myClickHandler01, and myClickHandler02 (Note that these two methods must have a View of the shape parameter).

1
  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 wording:

The two buttons set to the same Listener

android: onClick = "myClickHandler"

android: onClick = "myClickHandler"

1
  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;)))

Refer to the article: "UI framework changes in Android 1.6" (take over the wall)