There are three ways to add events to a button in Android, and here's a summary, of course, it's completely Java based.
1. Internal class:
?
Code fragment, double-click copy
Btn.setonclicklistener (New Onclicklistener ()
{
public void OnClick (View v)
{
...
}
});
This method is suitable for only a single button to add events, when the button is more, it is necessary to write the OnClick () method, this is not the best in practice.
2, create a separate class:
?
Code fragment, double-click copy
Btn.setonclicklistener (New MyListener ());
Class MyListener implements Onclicklistener
{
public void OnClick (View v)
{
...
}
}
This practice is similar to that of an internal class, and the general practice does not require a separate declaration of a class. Instead, the inner class hides the implementation part from the class. Of course, this is better than the internal class is able to reuse.
3. Implement Interface only
?
Code fragment, double-click copy
Btn.setonclicklistener (listener);
Onclicklistener listener = new Onclicklistener ()
{
public void OnClick (View v)
{
...
}
};
This saves code, and when there are multiple buttons, you can use a listener to reduce the call to the OnClick () method. And just in the onclick () method to determine which button is OK.
?
Code fragment, 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
...
}
}
};
Android photo, video, audio code example
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;//Photography Requestcode
private static final int request_code_take_video = 2;//Photography Requestcode
private static final int result_capture_recorder_sound = 3;//recording Requestcode
Private String Strimgpath = "";//Photo file absolute Path
Private String Strvideopath = "";//absolute path of video file
Private String Strrecorderpath = "";//absolute path of 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://Photo shoot
if (ResultCode = = RESULT_OK) {
Toast.maketext (This, Strimgpath, Toast.length_short). Show ();
}
Break
Case request_code_take_video://Shooting Video
if (ResultCode = = RESULT_OK) {
Uri Urivideo = Data.getdata ();
Cursor cursor=this.getcontentresolver (). query (urivideo, NULL, NULL, NULL, NULL);
if (Cursor.movetonext ()) {
/* _data: The absolute path of the file, _display_name: filename * *
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, NULL, NULL, NULL);
if (Cursor.movetonext ()) {
/* _data: The absolute path of the file, _display_name: filename * *
Strrecorderpath = cursor.getstring (Cursor.getcolumnindex ("_data"));
Toast.maketext (This, Strrecorderpath, Toast.length_short). Show ();
}
}
Break
}
}
/**
* Photographic function
*/
private void Cameramethod () {
Intent imagecaptureintent = new Intent (mediastore.action_image_capture);
Strimgpath = Environment.getexternalstoragedirectory (). toString () + "/consdcgmpic/";//folder where photos are stored
String fileName = new SimpleDateFormat ("YYYYMMDDHHMMSS"). Format (new Date ()) + ". jpg";//Photo naming
File out = new file (Strimgpath);
if (!out.exists ()) {
Out.mkdirs ();
}
out = new File (Strimgpath, fileName);
Strimgpath = strimgpath + filename;//absolute path to this photo
Uri uri = Uri.fromfile (out);
Imagecaptureintent.putextra (Mediastore.extra_output, URI);
Imagecaptureintent.putextra (mediastore.extra_video_quality, 1);
Startactivityforresult (Imagecaptureintent, result_capture_image);
}
/**
* Shooting Video
*/
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"/>