The. XML code is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" > <analogclock android:id= "@+id/myanalogclock" android:layout_width= " Wrap_content " android:layout_height=" wrap_content "/> <textview android:id=" @+id/info " Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= ""/></ Linearlayout>
The. Java program code is as follows:
Package Org.lxh.demo;import Java.util.date;import Java.text.simpledateformat;import android.app.activity;import Android.os.bundle;import Android.os.handler;import Android.os.message;import Android.widget.textview;public class Hello extends Activity {private TextView info = null;private static final int SET = 1;private Handler Handler = new Handle R () {@Overridepublic void Handlemessage (Message msg) {switch (msg.what) {case SET:Hello.this.info.setText ("Current time is:" + Msg.obj.toString ()); break;}}; Private class Clockthread implements Runnable {//Display time thread class public void run () {while (true) {try {Message msg = Hello.this.ha Ndler.obtainmessage (hello.set,new simpledateformat ("Yyyy-mm-dd HH:mm:ss"). Format (new Date ())); Hello.this.handler.sendMessage (msg); Thread.Sleep (1000);} catch (Exception e) {}}}}public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);// Life cycle Method Super.setcontentview (R.layout.main); Set the layout manager to use This.info = (TextView) Super.findviewbyid (r.id.info); new ThreaD (New Clockthread ()). Start ();//Start Thread}} Run the following example:
Android-interactive instance (clock) between the child thread and the main thread