如何在android中显示每秒动态变化的当前时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10634231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Display current time that changes dynamically for every second in android
提问by Navamani Samuel
I need to display the current time in a textview and the time changes dynamically for every second (Like the digital clock) in android.. I have googled but i didnt get the help.If Anyone have the idea for this, please help me Thanks in advance.
我需要在 textview 中显示当前时间,并且时间在 android 中每秒动态变化(如数字时钟)。我用谷歌搜索但没有得到帮助。如果有人对此有想法,请帮助我谢谢提前。
采纳答案by Ayyappan
Use CountDownTimer class , pass a huge value to (here I gave 1000000000) for CountDownTimer life .
使用 CountDownTimer 类,为 CountDownTimer life 传递一个巨大的值(这里我给了 1000000000)。
Sample Code::
示例代码::
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();
While closing the Activity, call the following function
关闭活动时,调用以下函数
newtimer.cancel();
回答by Bhavin
This is the Code..
这是代码..
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){
}
}
}
}
回答by Nikhil Dinesh
Use TextClock. this wil change dynamically without having a separate code.
使用文本时钟。这将动态更改而无需单独的代码。
<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");
回答by Lucifer
You can use ThreadClass with sleep(1000); or you can use TimerTaskClass.
您可以将ThreadClass 与 sleep(1000) 一起使用;或者你可以使用TimerTask类。
回答by Gaurav Agarwal
Use Chronometerclass in Android.
Chronometer在 Android 中使用类。
回答by Dhruvil Patel
for this you should try Chronometer, by this you can fulfill your goal good luck
为此你应该尝试天文台,这样你就可以实现你的目标祝你好运
sample code of Chronometer is here :
Chronometer 的示例代码在这里:
in main.xml
在 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>
in java file
在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());
}});
}
}

