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