13

我需要在文本视图中显示当前时间,并且时间在 android 中每秒动态变化(如数字时钟)。我已经用谷歌搜索但我没有得到帮助。如果有人对此有想法,请帮助我谢谢提前。

4

6 回答 6

25

这是代码..

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    Thread myThread = null;

    Runnable myRunnableThread = new CountDownRunner();
    myThread= new Thread(myRunnableThread);   
    myThread.start();

}

public void doWork() {
    runOnUiThread(new Runnable() {
        public void run() {
            try{
                TextView txtCurrentTime= (TextView)findViewById(R.id.myText);
                    Date dt = new Date();
                    int hours = dt.getHours();
                    int minutes = dt.getMinutes();
                    int seconds = dt.getSeconds();
                    String curTime = hours + ":" + minutes + ":" + seconds;
                    txtCurrentTime.setText(curTime);
            }catch (Exception e) {}
        }
    });
}


class CountDownRunner implements Runnable{
    // @Override
    public void run() {
            while(!Thread.currentThread().isInterrupted()){
                try {
                doWork();
                    Thread.sleep(1000); // Pause of 1 Second
                } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                }catch(Exception e){
                }
            }
    }
}
于 2012-05-17T10:54:48.563 回答
18

使用文本时钟。这将动态更改而无需单独的代码。

        <TextClock
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/textClock"
            android:layout_weight="0.11"
            android:textStyle="bold"
            android:textSize="22sp"
            android:format24hours="hh:mm:ss a  EEE MMM d"/>

    TextClock textClock = (TextClock) findViewById(R.id.textClock);
    textClock.setFormat12Hour(null);
    //textClock.setFormat24Hour("dd/MM/yyyy hh:mm:ss a");
    textClock.setFormat24Hour("hh:mm:ss a  EEE MMM d");
于 2016-12-03T19:46:20.083 回答
15

使用 CountDownTimer 类,为 CountDownTimer life 传递一个巨大的值(这里我给了 1000000000)。

示例代码::

CountDownTimer newtimer = new CountDownTimer(1000000000, 1000) { 

            public void onTick(long millisUntilFinished) {
                Calendar c = Calendar.getInstance();
                textView.setText(c.get(Calendar.HOUR)+":"+c.get(Calendar.MINUTE)+":"+c.get(Calendar.SECOND));
            }
            public void onFinish() {

            }
        };
        newtimer.start();

在关闭 Activity 时,调用以下函数

newtimer.cancel();
于 2015-09-10T11:30:58.303 回答
6

您可以将ThreadClass 与 sleep(1000) 一起使用;或者你可以使用TimerTask类。

于 2012-05-17T10:54:43.833 回答
5

Chronometer在 Android 中使用类。

于 2012-05-17T10:46:34.120 回答
2

为此,您应该尝试 Chronometer,这样您就可以实现您的目标,祝您好运

Chronometer 的示例代码在这里:

在 main.xml

<?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"
  >
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<Chronometer
 android:id="@+id/chronometer"
 android:layout_gravity="center_horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
 />
<Button
 android:id="@+id/buttonstart"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Start"
 />
<Button
 android:id="@+id/buttonstop"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Stop"
 />
<Button
 android:id="@+id/buttonreset"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Reset"
 />
</LinearLayout>

在java文件中

package com.exercise.AndroidChronometer;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;

public class AndroidChronometer extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       final Chronometer myChronometer = (Chronometer)findViewById(R.id.chronometer);
       Button buttonStart = (Button)findViewById(R.id.buttonstart);
       Button buttonStop = (Button)findViewById(R.id.buttonstop);
       Button buttonReset = (Button)findViewById(R.id.buttonreset);

       buttonStart.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    myChronometer.start();
   }});

       buttonStop.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    myChronometer.stop();

   }});

       buttonReset.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    myChronometer.setBase(SystemClock.elapsedRealtime());

   }});


   }
}
于 2012-05-17T10:55:59.480 回答