Android interface Jump, is actually a one of the activity, below to see how to create a new activity layout
On the layout folder in the project res, right-click the Android XML layout file under the other------Android folder
Next, then write the file name, Root Element: Select a layout, then finish to create a successful
You can drag a button over the interface in Form Widgets
Double-clicking the button will enter the source code, which will automatically generate the interface layout codes for the button just clicked.
And then in manactivity to load this resource layout, through Setcontentview This method to load the resource layout, passed r.layout.test this layout
R.layout.test actually created the XML file in layout earlier so that the layout can be loaded
Public class extends Activity { @Override protectedvoid onCreate (Bundle Savedinstancestate) { super. OnCreate (savedinstancestate); // Setcontentview (r.layout.activity_main); Setcontentview (r.layout.test); }}
Then run, load successfully
Here, I want to do things, click Button, I found nothing effect, I want to be like JavaScript pop-up information out of the same, point for a half a day not response, JavaScript has a click event,
Here should also have, sure enough, there are methods as follows
Method One: In the Mainactivity method OnCreate, through the Findviewbyid () method to find the layout of the control, through the Button's Setonclicklistener () method to pass in a
Event Listener Interface Onclicklistener interface, here is the method of anonymous inner class, implement the OnClick (View V) This method can be
PrivateButton btn1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);//Setcontentview (r.layout.activity_main);Setcontentview (r.layout.test); BTN1=(Button) Findviewbyid (R.id.testbutton1); Btn1.settext ("Point me."); Btn1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {/*** Toast is similar to a bullet box * Context: Contextual, refers to Activity, so write Mainactivity.this. * Text: Textual content * Duration: time, int type value*/Toast.maketext (mainactivity. This, "Hello Android", 1). Show (); } }); }
Then look at the effect:
Method Two:
As before, the difference is that an implementation class is passed in the Setonclicklistener () method of the Button.
Create a new class to implement the Onclicklistener interface
Public class Implements Onclicklistener { private context context; Public Btn1clicklistener (Context context) { this. Context= context; } @Override publicvoid OnClick (View v) { "Hello Android 1 class", 1). Show (); }}
You can use it directly in the mainactivity.
protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);//Setcontentview (r.layout.activity_main);Setcontentview (r.layout.test); /* *//*** Second Method*/BTN1=(Button) Findviewbyid (R.id.testbutton1); Btn1.settext ("Point me."); //Just go to the new instance and pass in the content.Btn1.setonclicklistener (NewBtn1clicklistener ( This)); }
The effect is as follows:
Android ch03 activity_layout and Button click events