|
|
@@ -0,0 +1,62 @@
|
|
|
+package com.ht.gate;
|
|
|
+
|
|
|
+import android.annotation.SuppressLint;
|
|
|
+import android.content.Context;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Message;
|
|
|
+import android.util.AttributeSet;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+import androidx.annotation.Nullable;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Locale;
|
|
|
+import java.util.concurrent.Executors;
|
|
|
+import java.util.concurrent.ScheduledExecutorService;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+@SuppressLint("AppCompatCustomView")
|
|
|
+public class TimeView extends TextView {
|
|
|
+ private ScheduledExecutorService executorService;
|
|
|
+ private Handler handler = new Handler(new Handler.Callback() {
|
|
|
+ @Override
|
|
|
+ public boolean handleMessage(@NonNull Message msg) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.CHINA);
|
|
|
+ setText(sdf.format(new Date()));
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ public TimeView(Context context) {
|
|
|
+ super(context);
|
|
|
+ initTimer();
|
|
|
+ }
|
|
|
+
|
|
|
+ public TimeView(Context context, @Nullable AttributeSet attrs) {
|
|
|
+ super(context, attrs);
|
|
|
+ initTimer();
|
|
|
+ }
|
|
|
+
|
|
|
+ public TimeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
|
|
+ super(context, attrs, defStyleAttr);
|
|
|
+ initTimer();
|
|
|
+ }
|
|
|
+
|
|
|
+ public TimeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
|
|
+ super(context, attrs, defStyleAttr, defStyleRes);
|
|
|
+ initTimer();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initTimer() {
|
|
|
+ executorService = Executors.newScheduledThreadPool(2);
|
|
|
+ executorService.scheduleAtFixedRate(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ handler.sendEmptyMessage(0);
|
|
|
+ }
|
|
|
+ }, 0, 1000, TimeUnit.MILLISECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|