Android 显示当前时间
在Android开发中,显示当前时间是一个非常常见的需求。无论是在时钟应用程序中,还是在其他需要显示时间的应用程序中,都会用到显示当前时间的功能。本文将介绍如何在Android应用程序中显示当前时间,并提供相应的代码示例。
1. 获取当前时间
要显示当前时间,首先需要获取当前的时间。在Android中,可以使用Calendar
类来获取当前时间。下面是一个获取当前时间的示例代码:
import java.util.Calendar;
public class CurrentTimeUtil {
public static String getCurrentTime() {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
return String.format("%02d:%02d:%02d", hour, minute, second);
}
}
上述代码中,getCurrentTime
方法使用Calendar.getInstance()
获取当前的Calendar
对象,然后使用get
方法获取当前的小时、分钟和秒钟,并使用String.format()
方法格式化时间字符串。
2. 在布局文件中显示时间
获取当前时间后,需要将其显示在界面上。在Android中,可以使用TextView
控件来显示文本信息。下面是一个布局文件的示例代码,用于显示当前时间:
<RelativeLayout xmlns:android="
xmlns:tools="
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/current_time_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_centerInParent="true" />
</RelativeLayout>
上述代码中,使用TextView
控件来显示当前时间。设置android:id
属性为current_time_text
,用于在Java代码中找到该控件。设置android:layout_width
和android:layout_height
属性为wrap_content
,以适应文本内容的大小。设置android:textSize
属性为24sp
,以调整文本的字体大小。设置android:layout_centerInParent
属性为true
,以将TextView
控件居中显示在父容器中。
3. 更新当前时间
在布局文件中设置好显示当前时间的TextView
后,需要在Java代码中更新时间。可以使用定时器(Timer
)来定时更新当前时间,并更新TextView
的文本内容。下面是一个定时更新当前时间的示例代码:
import android.os.Handler;
import android.os.Looper;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class CurrentTimeUpdater {
private Timer mTimer;
private TextView mTextView;
private Handler mHandler;
public CurrentTimeUpdater(TextView textView) {
mTextView = textView;
mHandler = new Handler(Looper.getMainLooper());
}
public void startUpdating() {
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
mHandler.post(new Runnable() {
@Override
public void run() {
String currentTime = CurrentTimeUtil.getCurrentTime();
mTextView.setText(currentTime);
}
});
}
}, 0, 1000);
}
public void stopUpdating() {
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
}
}
上述代码中,CurrentTimeUpdater
类用于定时更新当前时间。在构造方法中,传入要更新时间的TextView
对象,并创建一个Handler
对象,用于在UI线程中更新界面。startUpdating
方法使用Timer
类定时更新时间,stopUpdating
方法用于停止更新时间。
4. 在Activity中使用当前时间
在Activity中使用当前时间,需要在onCreate
方法中初始化布局,并在onResume
方法中开始更新时间,在onPause
方法中停止更新时间。下面是一个Activity的示例代码:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView mCurrentTimeText;
private CurrentTimeUpdater mTimeUpdater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCurrentTimeText = findViewById(R.id.current_time_text);
mTimeUpdater = new CurrentTimeUpdater(mCurrentTimeText);
}
@Override
protected void onResume() {
super.onResume();
mTimeUpdater.startUpdating();