我想创建一个时钟,显示当前时间的小时和分钟,如02:15 PM。我现在想要的是保持分钟部分更新明显后,每60秒,因为它是在我们的系统改变。所以我想知道保持时间更新的最好方法是什么由于
.xml文件的代码,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/txtTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
活动文件的代码,
package com.example.demoproject;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.text.format.Time;
import android.widget.TextView;
public class MainActivity extends Activity
{
private static TextView textview;
private Timer timer;
private Time today;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView ) findViewById( R.id.txtTime );
today = new Time(Time.getCurrentTimezone());
timer = new Timer();
timer.schedule(new RemindTask(), 1000, 1000 );
}
private class RemindTask extends TimerTask
{
public void run()
{
runOnUiThread(new Runnable()
{
public void run()
{
today.setToNow();
textview.setText(today.format("%k:%M:%S")); // Current time
}
});
}
}
}