1. Click on the event to achieve several ways
Button Btn1 = (button) Findviewbyid (R.id.button1); // Get button Resource Btn1.setonclicklistener (new Button.onclicklistener () {//Create a listener for this button , Create an instance of Button.onclicklistener this class public void OnClick (View v) {// If there is a button operation, execute onclick= "click Button01"; Ev1.settext (strtmp); Eject strtmp }
The second method:
In component file Main.xml, add to button:
android:onclick= "Onclick_event"
Add the following code to the activity:
public void onclick_event (view view) {
TextView TextView = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Textview.settext ("You clicked Button");
}
The role of parameters in 2.oncreate
Super. OnCreate (savedinstancestate);
In the activity life cycle, as long as you leave the visible phase, or lose focus, theactivity is likely to be terminated by the process! , was killed, , at this time, there is a mechanism, can save the state of the moment, this is the role of Savedinstancestate, in short, is to save the activity before the interface.
Setcontentview (R.layout.activity_main);
Loads the UI component from the Res/layout/activity_main.xml file.
3.
ImageView ImageView = (ImageView) Findviewbyid (R.id.myimageview);
Gets the button in the XML in the activity, the parameter is the ID of the button
Android Re-learning