OnClick Event
Both 1.Button and ImageButton have an onclick event
Add a click event through your own. Setonclicklistener (Onclicklistener) method
2. All controls have an OnClick event
Not only button and ImageButton have
3. Click on the event of the listener can achieve the Click button after the action of what to happen
A centralized notation for monitoring event implementations
1. Implementation of anonymous inner classes
2. Implementation of independent classes
3. Implement the interface way to achieve
PackageCom.example.buttonimagebutton;ImportAndroid.graphics.Color;ImportAndroid.os.Bundle;Importandroid.support.v4.app.Fragment;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.ViewGroup;ImportAndroid.widget.Button;ImportAndroid.widget.ImageButton;ImportAndroid.widget.Toast; Public classMainactivityextendsactionbaractivity {PrivateButton button1; PrivateImageButton ImageButton1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //button1 implementing anonymous internal classesButton1 =(Button) Findviewbyid (R.id.button1); Button1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {intRed = (int) (Math.random () * 255.9); intGreen = (int) (Math.random () * 255.9); intBlue = (int) (Math.random () * 255.9); Button1.setbackgroundcolor (Color.rgb (red, green, blue)); } }); //Button2 Implementing an external class MyonclicklistenerImageButton1 =(ImageButton) Findviewbyid (R.id.imagebutton1); Imagebutton1.setonclicklistener (NewMyonclicklistener ( This)); }}classMyonclicklistenerImplementsOnclicklistener {Privatemainactivity context; PublicMyonclicklistener (mainactivity context) { This. Context =context; } @Override Public voidOnClick (View arg0) {toast.maketext (context,"Image button (ImageButton) to execute the logic", 1). Show (); }}
Mainactivity.java
Where button1 is used to implement an anonymous inner class, its role is that whenever a click button1 its background color is random change;
ImageButton1 Class for Implementing independent (custom) listener events (Myonclicklistener implements Onclicklistener), which is used to display a hint message (Toast) in mainactivity.
<LinearLayoutXmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:orientation= "Vertical"><ButtonAndroid:id= "@+id/button1" Android:layout_width = "match_parent" Android:layout_height= "Wrap_content" Android:text= "button 1" /> <imagebutton android:id= "@+id/imagebutton1" Android:layout_width=" match_parent " Android:layout_height= "wrap_content" android:src< Span style= "color: #0000ff;" >= "@drawable/ic_launcher" /></linearlayout>
Activity_main.xml
Effect:
Click events for Android Monitor button