欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)顯示系統(tǒng)實(shí)時時間

 更新時間:2021年05月10日 15:15:34   作者:AaVictory.  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)顯示系統(tǒng)實(shí)時時間,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

Android顯示系統(tǒng)實(shí)時時間的具體代碼,供大家參考,具體內(nèi)容如下

獲取系統(tǒng)當(dāng)前時間 System.currentTimeMillis(); 需要開啟一個線程,我們通過Handler來實(shí)現(xiàn)實(shí)時更新時間

效果圖

Activity.xml代碼

<TextView
        android:id="@+id/real_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-- --"
        />

MainActivity代碼

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        real_time=findViewById(R.id.real_time);
        Startthread();
    }
 //開啟一個子線程
  private void Startthread(){
        new Thread(){
            @Override
            public void run() {
                do {
                    try {
                        Thread.sleep(1000);
                        Message message=new Message();
                        message.what=1;
                        handler.sendMessage(message);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }while (true);
            }
        }.start();
     }

//在主線程中進(jìn)行數(shù)據(jù)處理
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 1:
                    long time = System.currentTimeMillis();
                    CharSequence format = DateFormat.format("hh:mm:ss yyyy-MM-dd", time);
                    real_time.setText(format);
                    break;
            }
        }
    };

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論