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_widthandroid: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();