<Android of training research project six> Time Messaging

Write an Android application requiring real-time applications can be dynamically displayed, press the phone's "Menu" button, pop-up and "Exit System" menu items "on the system." Press the "Exit System" menu item, exit the application. Handler is required to achieve message passing between threads.

Project Screenshot

 1 package com.example.time;
 2 
 3 import android.os.Handler;
 4 import android.os.Message;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.os.Bundle;
 7 import android.view.Menu;
 8 import android.view.MenuItem;
 9 import android.widget.TextView;
10 
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
13 
14 public class MainActivity extends AppCompatActivity {
15 
16     private TextView tv;
17     private Handler handler;
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23         tv = (TextView)findViewById(R.id.tv);
24         handler = new Handler(){
25             @Override
26             public void dispatchMessage(Message msg) {
27                 super.dispatchMessage(msg);
28                 if(msg.what==1)
29                 {
30                     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
31                     String time = simpleDateFormat.format(new Date());
32                     tv.setText(time);
33                 }
34             }
35         };
36         new Thread(){
37             @Override
38             public void run() {
39                 super.run();
40                 while(true){
41                     try{
42                         Thread.sleep(1000);
43                         Message msg = new Message();
44                         msg.what=1;
45                         handler.sendMessage(msg);
46                     }
47                     catch(Exception e)
48                     {
49                         e.printStackTrace();
50                     }
51                 }
52             }
53         }.start();
54     }
55 
56     @Override
57     public boolean onCreateOptionsMenu(Menu menu) {
58         menu.add(1,1,1,"关于本系统");
59         menu.add(1,2,2,"退出系统");
60         return super.onCreateOptionsMenu(menu);
61     }
62 
63     @Override
64     public boolean onOptionsItemSelected(MenuItem item) {
65         switch (item.getItemId())
66         {
67             case 1:
68                 break;
69             case 2:
70                 finish();
71                 break;
72             default:
73                 break;
74         }
75         return super.onOptionsItemSelected(item);
76     }
77 }
MainActivity.java
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity">
 8 
 9     <TextView
10         android:id="@+id/tv"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="Hello World!"
14         android:textSize="30sp"
15         app:layout_constraintBottom_toBottomOf="parent"
16         app:layout_constraintLeft_toLeftOf="parent"
17         app:layout_constraintRight_toRightOf="parent"
18         app:layout_constraintTop_toTopOf="parent" />
19 
20 </android.support.constraint.ConstraintLayout>
activity_main.xml

 

Guess you like

Origin www.cnblogs.com/jdxb/p/10941634.html