我需要在textview中显示当前时间,并且动态时间每秒动态变化(如数字时钟)在Android中..我用Google搜索但我没有得到帮助。如果有人有这个想法,请帮我 提前谢谢。
答案 0 :(得分: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){
}
}
}
}
答案 1 :(得分:10)
使用CountDownTimer类,为CountDownTimer生命传递一个巨大的值(这里我给了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();
关闭活动时,请调用以下函数
newtimer.cancel();
答案 2 :(得分:5)
你可以使用Thread
Class with sleep(1000);或者您可以使用TimerTask
类。
答案 3 :(得分:4)
在Android中使用Chronometer
课程。
答案 4 :(得分:1)
为此你应该尝试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());
}});
}
}