Android uses one listener to implement multiple listeners

Source: Internet
Author: User

In android applications, many button components are sometimes used. When no button is used, a listening event is required. To make the Code look clean and concise, and save some memory, we can use a Listener (Listener) to implement onClick listening for multiple buttons. The following is an example:
[Java] package com. android;
 
Import android. app. Activity;
Import android. content. Intent;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
 
Public class IntentSelectActivity extends Activity implements View. OnClickListener {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Button button1 = (Button) findViewById (R. id. btn1 );
Button button2 = (Button) findViewById (R. id. btn2 );
Button button3 = (Button) findViewById (R. id. btn3 );
Button1.setOnClickListener (this );
Button1.setTag (1 );
Button2.setOnClickListener (this );
Button2.setTag (2 );
Button3.setOnClickListener (this );
Button3.setTag (3 );

 
}
Public void onClick (View v ){
Int tag = (Integer) v. getTag ();
Switch (tag ){
Case 1:
Intent music = new Intent (Intent. ACTION_GET_CONTENT );
Music. setType ("audio /*");
StartActivity (Intent. createChooser (music, "Select music "));
Break;
Case 2:
Intent dial = new Intent ();
Dial. setAction ("android. intent. action. CALL ");
Dial. setData (Uri. parse ("tel: 13428720000 "));
StartActivity (dial );
Break;
Case 3:
Intent wallpaper = new Intent (Intent. ACTION_SET_WALLPAPER );
StartActivity (Intent. createChooser (wallpaper, "Select Wallpaper "));
Break;
Default:
Break;
}
 
}
}
Package com. android;

Import android. app. Activity;
Import android. content. Intent;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;

Public class IntentSelectActivity extends Activity implements View. OnClickListener {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Button button1 = (Button) findViewById (R. id. btn1 );
Button button2 = (Button) findViewById (R. id. btn2 );
Button button3 = (Button) findViewById (R. id. btn3 );
Button1.setOnClickListener (this );
Button1.setTag (1 );
Button2.setOnClickListener (this );
Button2.setTag (2 );
Button3.setOnClickListener (this );
Button3.setTag (3 );

}
Public void onClick (View v ){
Int tag = (Integer) v. getTag ();
Switch (tag ){
Case 1:
Intent music = new Intent (Intent. ACTION_GET_CONTENT );
Music. setType ("audio /*");
StartActivity (Intent. createChooser (music, "Select music "));
Break;
Case 2:
Intent dial = new Intent ();
Dial. setAction ("android. intent. action. CALL ");
Dial. setData (Uri. parse ("tel: 13428720000 "));
StartActivity (dial );
Break;
Case 3:
Intent wallpaper = new Intent (Intent. ACTION_SET_WALLPAPER );
StartActivity (Intent. createChooser (wallpaper, "Select Wallpaper "));
Break;
Default:
Break;
}

}
}

This Code uses three buttons to implement three Intent intentions: music playback, automatic dialing, and background selection. Only one onClick is used for processing, so the code looks much simpler.

Note: Intent attribute writing and constant writing:


Attribute writing
Intent dial = new Intent ();
Dial. setAction ("android. intent. action. CALL ");
Constant statement www.2cto.com
Intent wallpaper = new Intent (Intent. ACTION_SET_WALLPAPER );
Intent music = new Intent (Intent. ACTION_GET_CONTENT );
In the Intent class, the constant of action is defined. In terms of memory skills, you can use xxx to correspond to ACTION_xxx. For example:

CALL (android. intent. action. CALL) is ACTION_CALL (Intent. ACTION_CALL ).

The program running effect is:

 








From Young's column

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.