diff --git a/alarmclockview/src/main/java/com/alarmclockview/AlarmClockView.java b/alarmclockview/src/main/java/com/alarmclockview/AlarmClockView.java index 665de7b..40d9011 100644 --- a/alarmclockview/src/main/java/com/alarmclockview/AlarmClockView.java +++ b/alarmclockview/src/main/java/com/alarmclockview/AlarmClockView.java @@ -49,9 +49,9 @@ public class AlarmClockView extends View { private int mYear; private boolean mIsShowTime; private String mWeekStr; + private TimeChangeListener listener; private Handler mHandler = new Handler(); - private Runnable runnable = new Runnable() { @Override public void run() { @@ -152,7 +152,7 @@ private void drawCurrentTime(Canvas canvas) { Paint.FontMetricsInt fm = mPaint.getFontMetricsInt(); int baseLineY = mCenterY + mOuterRadius - fm.top + 2 * mSpace; - String time = "" + mYear + "年" + (mMonth + 1) + "月" + mDay + "日," + mWeekStr + "," + mHour + "点" + mMinute + "分" + mSecond + "秒"; + String time = "" + mYear + "年" + (mMonth + 1) + "月" + mDay + "日" + mWeekStr + mHour + "点" + mMinute + "分" + mSecond + "秒"; canvas.drawText(time, mCenterX, baseLineY, mPaint); } @@ -327,10 +327,14 @@ public void initCurrentTime() { System.out.println("现在时间:小时:" + mHour + ",分钟:" + mMinute + ",秒:" + mSecond); + if (listener != null) { + listener.onTimeChange(mCalendar); + } invalidate(); } - public void start() { + public void start(TimeChangeListener listener) { + this.listener = listener; mHandler.postDelayed(runnable, 1000); initCurrentTime(); } diff --git a/alarmclockview/src/main/java/com/alarmclockview/TimeChangeListener.java b/alarmclockview/src/main/java/com/alarmclockview/TimeChangeListener.java new file mode 100644 index 0000000..fffe7fe --- /dev/null +++ b/alarmclockview/src/main/java/com/alarmclockview/TimeChangeListener.java @@ -0,0 +1,11 @@ +package com.alarmclockview; + +import java.util.Calendar; + +/** + * Created by HARRY on 2019/1/4 0004. + */ + +public interface TimeChangeListener { + void onTimeChange(Calendar calendar); +} diff --git a/app/src/main/java/com/alarmclockview/MainActivity.java b/app/src/main/java/com/alarmclockview/MainActivity.java index 0fd77be..6da15bf 100644 --- a/app/src/main/java/com/alarmclockview/MainActivity.java +++ b/app/src/main/java/com/alarmclockview/MainActivity.java @@ -4,6 +4,8 @@ import android.os.Bundle; import android.view.View; +import java.util.Calendar; + public class MainActivity extends AppCompatActivity { private AlarmClockView mClock; @@ -14,11 +16,12 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); mClock = findViewById(R.id.clock); - - mClock.setOnClickListener(new View.OnClickListener() { + //运行闹钟 + mClock.start(new TimeChangeListener() { @Override - public void onClick(View v) { - mClock.start(); + public void onTimeChange(Calendar calendar) { + //根据calendar获取当前时间 + } }); }