首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在android中显示每秒动态变化的当前时间

问如何在android中显示每秒动态变化的当前时间
EN

Stack Overflow用户
提问于 2012-05-17 18:45:14
回答 6查看 29.4K关注 0票数 13

我需要在文本视图中显示当前时间,并且android中的时间每秒(如数字时钟)都会动态变化。我已经谷歌,但我没有得到的help.If谁有这方面的想法,请帮助我提前感谢。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2015-09-10 19:30:58

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

示例代码::

代码语言:javascript
复制
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();

在关闭活动时,调用以下函数

代码语言:javascript
复制
newtimer.cancel();
票数 15
EN

Stack Overflow用户

发布于 2012-05-17 18:54:49

这是代码..

代码语言:javascript
复制
    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){
                }
            }
    }
}
票数 25
EN

Stack Overflow用户

发布于 2016-12-04 03:46:20

使用TextClock。这将在没有单独代码的情况下动态变化。

代码语言:javascript
复制
        <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");
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10634231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档