Method 1: embed the following code in the oncreate () method of the activity:
Button button = (button) findviewbyid (R. Id. button1 );
Button. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view arg0 ){
Textview = (textview) findviewbyid (R. Id. textview1 );
Textview. settext ("button ");
}
});
In this way, the findviewbyid is used to instantiate the button through ID. Then, the listener object of the button is set, and The onclick () method of onclicklistenter is also implemented. There is not much code in this method, but in Java, the object-oriented idea cannot be modularized about coupling. Code is stacked together and bloated.
Method 2: Let acticity hold textview, and create a class of button_listener to implement the onclicklistener interface. The code in the activity is:
Public class testonclickactivity extends activity {
Private textview;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Button button = (button) findviewbyid (R. Id. button1 );
This. textview = (textview) findviewbyid (R. Id. textview1 );
Button. setonclicklistener (New button_listener (this ));
}
Public textview gettextview (){
Return this. textview;
}
}
The code for the class button_listener is:
Class button_listener implements onclicklistener {
Private testonclickactivity activity;
Public button_listener (testonclickactivity activity ){
This. Activity = activity;
}
@ Override
Public void onclick (view v ){
Textview = activity. gettextview ();
Textview. settext ("You clicked button ");
}
}
Method 2:
In method 2, in order to correct the non-standard implementation of method 1 interfaces, a class called button_listener is specially re-established.
In this way, the code structure is clear. To add a listener to a button, you only need new button_listener (this. But at the same time, we can also find that because it does not belong to the testonclickactivity class, the button_listener class must use testonclickactivity as a member. In addition, we need to add textview to the display in testonclickactivity and write the gettextview () method to implement our program intent.
Although the Code is modularized and decoupled to a certain extent, the result is that the Code is more complicated to write.
Method 3: Add button to the main. xml component file:
Android: onclick = "onclick_event"
Add the following code to the activity:
Public void onclick_event (view ){
Textview = (textview) findviewbyid (R. Id. textview1 );
Textview. settext ("button ");
}
In the three methods, you only need to add a member function onclick_event () to testonclickactivity and add the description of onclick_event to the component file.
The code structure is simple, clear, and the amount of code is greatly reduced. The configurability of XML files increases the maintainability of the project and the modularization is further enhanced!
Reference: http://blog.sina.com.cn/s/blog_7013d1fe01014t3o.html