Android adds events to buttons in three ways
There are generally three methods for adding events to buttons in Android. Here we will summarize, of course, this is completely the basic content of java.
1. Internal class:
?
Code snippet, double-click Copy
Btn. setOnClickListener (new OnClickListener ()
{
Public void onClick (View v)
{
...
}
});
This method is suitable for adding events only for a single button. When there are many buttons, you must repeat the onClick () method. This is not the best practice.
2. Create an independent class:
?
Code snippet, double-click Copy
Btn. setOnClickListener (new MyListener ());
Class MyListener implements OnClickListener
{
Public void onClick (View v)
{
...
}
}
This approach is similar to the internal class practice. Generally, you do not need to declare a class separately. On the contrary, the internal class is used to hide the implementation part of the class. Of course, this is better than the internal class, that is, it can be reused.
3. Only implement interfaces
?
Code snippet, double-click Copy
Btn. setOnClickListener (listener );
OnClickListener listener = new OnClickListener ()
{
Public void onClick (View v)
{
...
}
};
This method can save code. When there are multiple buttons, you can use a listener to reduce the onClick () method call. In The onClick () method, you only need to determine the button.
?
Code snippet, double-click Copy
Btn1 = (Button) findViewById (R. id. btn1 );
Btn2 = (Button) findViewById (R. id. btn2 );
Btn1.setOnClickListener (listener );
Btn2.setOnClickListener (listener );
OnClickListener listener = new OnClickListener ()
{
Public void onClick (View v)
{
Btn = (Button) v;
Switch (btn. getId ())
{
Case R. id. btn1:
...;
Break;
Case R. id. btn2:
...;
Break;
...
}
}
};
Examples of Android photo taking, video recording, and Recording Code
Import java. io. File;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import android. app. Activity;
Import android. content. Intent;
Import android. database. Cursor;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. provider. MediaStore;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. Toast;
Public class ActivityMedia extends Activity implements OnClickListener {
Private static final int RESULT_CAPTURE_IMAGE = 1; // The requestCode of the photo.
Private static final int REQUEST_CODE_TAKE_VIDEO = 2; // The requestCode of the camera.
Private static final int RESULT_CAPTURE_RECORDER_SOUND = 3; // The requestCode of the recording
Private String strImgPath = ""; // absolute path of the photo file
Private String strVideoPath = ""; // absolute path of the video file
Private String strRecorderPath = ""; // absolute path of the recording file
Button buttonShot;
Button buttonVideo;
Button buttonRecorder;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
This. setContentView (R. layout. media );
ButtonShot = (Button) findViewById (R. id. ButtonShot );
ButtonShot. setOnClickListener (this );
ButtonVideo = (Button) findViewById (R. id. ButtonVideo );
ButtonVideo. setOnClickListener (this );
ButtonRecorder = (Button) findViewById (R. id. ButtonRecorder );
ButtonRecorder. setOnClickListener (this );
}
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
Switch (requestCode ){
Case RESULT_CAPTURE_IMAGE: // take a photo
If (resultCode = RESULT_ OK ){
Toast. makeText (this, strImgPath, Toast. LENGTH_SHORT). show ();
}
Break;
Case REQUEST_CODE_TAKE_VIDEO: // video shooting
If (resultCode = RESULT_ OK ){
Uri uriVideo = data. getData ();
Cursor cursor = this. getContentResolver (). query (uriVideo, null );
If (cursor. moveToNext ()){
/* _ Data: absolute path of the file, _ display_name: file name */
StrVideoPath = cursor. getString (cursor. getColumnIndex ("_ data "));
Toast. makeText (this, strVideoPath, Toast. LENGTH_SHORT). show ();
}
}
Break;
Case RESULT_CAPTURE_RECORDER_SOUND: // recording
If (resultCode = RESULT_ OK ){
Uri uriRecorder = data. getData ();
Cursor cursor = this. getContentResolver (). query (uriRecorder, null );
If (cursor. moveToNext ()){
/* _ Data: absolute path of the file, _ display_name: file name */
StrRecorderPath = cursor. getString (cursor. getColumnIndex ("_ data "));
Toast. makeText (this, strRecorderPath, Toast. LENGTH_SHORT). show ();
}
}
Break;
}
}
/**
* Photography
*/
Private void cameraMethod (){
Intent imageCaptureIntent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE );
StrImgPath = Environment. getExternalStorageDirectory (). toString () + "/CONSDCGMPIC/"; // folder for storing photos
String fileName = new SimpleDateFormat ("yyyyMMddHHmmss"). format (new Date () + ". jpg"; // name the photo
File out = new File (strImgPath );
If (! Out. exists ()){
Out. mkdirs ();
}
Out = new File (strImgPath, fileName );
StrImgPath = strImgPath + fileName; // the absolute path of the photo
Uri uri = Uri. fromFile (out );
ImageCaptureIntent. putExtra (MediaStore. EXTRA_OUTPUT, uri );
ImageCaptureIntent. putExtra (MediaStore. EXTRA_VIDEO_QUALITY, 1 );
StartActivityForResult (imageCaptureIntent, RESULT_CAPTURE_IMAGE );
}
/**
* Video shooting
*/
Private void videoMethod (){
Intent intent = new Intent (MediaStore. ACTION_VIDEO_CAPTURE );
Intent. putExtra (MediaStore. EXTRA_VIDEO_QUALITY, 0 );
StartActivityForResult (intent, REQUEST_CODE_TAKE_VIDEO );
}
/**
* Recording function
*/
Private void soundRecorderMethod (){
Intent intent = new Intent (Intent. ACTION_GET_CONTENT );
Intent. setType ("audio/amr ");
StartActivityForResult (intent, RESULT_CAPTURE_RECORDER_SOUND );
}
/**
* Prompt information
* @ Param text
* @ Param duration
*/
Private void showToast (String text, int duration ){
Toast. makeText (ActivityMedia. this, text, duration). show ();
}
Public void onClick (View v ){
Int id = v. getId ();
Switch (id ){
Case R. id. ButtonShot:
CameraMethod ();
Break;
Case R. id. ButtonVideo:
VideoMethod ();
Break;
Case R. id. ButtonRecorder:
SoundRecorderMethod ();
Break;
}
}
}
Copy code
Interface layout:
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
Android: id = "@ + id/ButtonShot"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "photo"/>
Android: id = "@ + id/ButtonVideo"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "video"/>
Android: id = "@ + id/ButtonRecorder"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Recording"/>