Learning a new language, of course, begins with helloworld. Now let's start our journey to helloworld for Android. If you don't talk much about it, simply go to the code. There are not many changes to this program, only the main activity and Main. XML files.
Package Snoopy. android. first; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. textview; public class helloworldactivity extends activity {// call back this method when this activity is created for the first time @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // sets the main. set page layout defined by the XML file Contentview (R. layout. main); // obtain the ID in the UI as R. id. OK button bn = (button) findviewbyid (R. id. OK); // bind a listener bn for the click event to the button. setonclicklistener (New onclicklistener () {public void onclick (view v) {// obtain the ID in the UI as R. id. show text box final textview show = (textview) findviewbyid (R. id. show); // change the text content of the text box show. settext ("Hello Android ~ "+ New java. util. Date ());}});}}
This is the corresponding activity file.
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<! -- Text box -->
<Textview Android: Id = "@ + ID/Show" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = ""/> <! -- Set the text of the button to "Click me" --> <button Android: text = "Click me" Android: Id = "@ + ID/OK" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> </linearlayout>
Main. xml file