In Android, the interface can be designed in advance in the. xml file, or dynamically generated in Java code.
Package Snoopy. android; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. view. viewgroup; import android. widget. button; import android. widget. linearlayout; import android. widget. textview; public class codeview extends activity {// this method is called back when this activity is created for the first time @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // create a linear layout manager linearlayout layout = new linearlayout (this); // set the activity to display layoutsuper. setcontentview (layout); layout. setorientation (linearlayout. vertical); // create a textviewfinal textview show = new textview (this); // create a button bn = new button (this); bn. settext (R. string. OK); // specifies the layout of length and height. wrap_content indicates the content to be adapted to bn. setlayoutparams (New viewgroup. layoutparams (viewgroup. layoutparams. wrap_content, viewgroup. layoutparams. wrap_content); // Add textviewlayout to the layout container. addview (show); // Add the button layout to the layout container. addview (BN); // bind an event listener bn to the button. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {Show. settext ("Hello, Android," + new Java. util. date ());}});}}
Program