Skip to content

Commit

Permalink
设置监听
Browse files Browse the repository at this point in the history
  • Loading branch information
hnsycsxhzcsh committed Jan 4, 2019
1 parent 24bc939 commit cad2dec
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
11 changes: 7 additions & 4 deletions app/src/main/java/com/alarmclockview/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.os.Bundle;
import android.view.View;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

private AlarmClockView mClock;
Expand All @@ -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获取当前时间

}
});
}
Expand Down

0 comments on commit cad2dec

Please sign in to comment.