From the beginning of 2015 to learn Android development, now the OnClick event implementation written down, recorded, for reference.
Implement button click Function, let TextView display a line of text, the simplest onclick event.
Direct Sticker Code:
Public void Onclick_method (view view) { String contextstring= "Click on this button to achieve the click function"; TextView TextView=(TextView) Findviewbyid (R.ID.TESTTV); Textview.settext (contextstring); }
Layout file:
<TextViewAndroid:id= "@+id/testtv"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "@string/hello_world"/> <ButtonAndroid:onclick= "Onclick_method"Android:id= "@+id/button"Android:layout_below= "@id/testtv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "click" />
Take a look at the following implementations:
Before clicking:
After clicking:
See there are other ways, but I feel this is the most simple and clear, later development and use of this can be.
Implementation of the OnClick event for Android development