From dba383a8245233f7ba09fe16b1132e83cdba39fc Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Sat, 20 Jan 2024 15:07:10 +0800 Subject: [PATCH] =?utf8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E7=95=8C=E9=9D=A2?= =?utf8?q?=E5=90=8C=E6=AD=A5=E6=97=B6=E9=97=B4=EF=BC=9A=E9=9C=80=E8=A6=81A?= =?utf8?q?T=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/dial/include/Digital_clockView.h | 34 ++++++++++++------- .../nativeui/dial/src/Digital_clockView.cpp | 33 +++++++++++------- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/code/sdk/application/wearable/nativeapp/nativeui/dial/include/Digital_clockView.h b/code/sdk/application/wearable/nativeapp/nativeui/dial/include/Digital_clockView.h index 08f91c750..880bd356c 100644 --- a/code/sdk/application/wearable/nativeapp/nativeui/dial/include/Digital_clockView.h +++ b/code/sdk/application/wearable/nativeapp/nativeui/dial/include/Digital_clockView.h @@ -75,18 +75,18 @@ namespace OHOS }; /* -------------------------------------------------------------------------- */ - /* -------------------------------------------------------------------------- */ class timeLableCallback : public AnimatorCallback { private: UILabel *timeLable{nullptr}; + UILabel *dateLable{nullptr}; + UILabel *weekLable{nullptr}; std::string stringtime; - struct timespec Systime; struct tm pTM; time_t seconds; public: - explicit timeLableCallback(UILabel *timeLable, std::string stringtime) : timeLable(timeLable), stringtime(stringtime) + explicit timeLableCallback(UILabel *timeLable, UILabel *dateLable, UILabel *weekLable) : timeLable(timeLable), dateLable(dateLable), weekLable(weekLable) { } ~timeLableCallback() {} @@ -94,22 +94,32 @@ namespace OHOS virtual void Callback(UIView *view) override { /* --------------------------------- 时间 --------------------------------- */ - clock_gettime(CLOCK_MONOTONIC, &Systime); - int SystemTime = Systime.tv_sec; - printf("--getSystemTime::%d--\n", SystemTime); - /* --------------------------------- 模拟更新时间 --------------------------------- */ // 获取系统时间 time(&seconds); localtime_r(&seconds, &pTM); - + /* -------------------------------------------------------------------------- */ // 解析小时和分钟 int hour = pTM.tm_hour; int minute = pTM.tm_min; - - // 将小时和分钟转换为字符串,并更新显示 - std::string stringtime = (hour < 10 ? "0" : "") + std::to_string(hour) + ":" + (minute < 10 ? "0" : "") + std::to_string(minute); + stringtime = (hour < 10 ? "0" : "") + std::to_string(hour) + ":" + (minute < 10 ? "0" : "") + std::to_string(minute); + // 将小时和分钟更新显示 timeLable->SetText(stringtime.c_str()); - printf("--SystemTime::%s--\n", stringtime.c_str()); + /* -------------------------------------------------------------------------- */ +#if 0 + // 解析年月日 + std::string dateStr = std::to_string(pTM.tm_year + 1900) + "-" + std::to_string(pTM.tm_mon + 1) + "-" + std::to_string(pTM.tm_mday); + // 解析星期 + std::string weekStr = getWeekString(pTM.tm_wday); + // 将日期更新显示 + dateLable->SetText(dateStr.c_str()); + // 将星期更新显示 + weekLable->SetText(weekStr.c_str()); +#endif + } + std::string getWeekString(int wday) + { + std::string weekDays[] = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; + return weekDays[wday]; } }; /* -------------------------------------------------------------------------- */ diff --git a/code/sdk/application/wearable/nativeapp/nativeui/dial/src/Digital_clockView.cpp b/code/sdk/application/wearable/nativeapp/nativeui/dial/src/Digital_clockView.cpp index 70194fdf5..7f35ff5fd 100644 --- a/code/sdk/application/wearable/nativeapp/nativeui/dial/src/Digital_clockView.cpp +++ b/code/sdk/application/wearable/nativeapp/nativeui/dial/src/Digital_clockView.cpp @@ -354,7 +354,6 @@ namespace OHOS timeLable->SetViewId("label1"); // Set style timeLable->SetStyle(STYLE_TEXT_FONT, digitsize); - timeLable->SetText(stringtime.c_str()); timeLable->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT); timeLable->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); @@ -367,7 +366,7 @@ namespace OHOS if (timeLableCallback_ == nullptr) { - timeLableCallback_ = new timeLableCallback(timeLable, stringtime); + timeLableCallback_ = new timeLableCallback(timeLable, dateLable, weekLable); timeLableanimator_ = new Animator(timeLableCallback_, timeLable, 1000, true); } timeLableanimator_->Start(); @@ -413,7 +412,6 @@ namespace OHOS sweepClock1_->SetStyle(STYLE_BACKGROUND_COLOR, 0xff000000); sweepClock1_->SetStyle(STYLE_BACKGROUND_OPA, 0x00); - sweepClock1_->SetWorkMode(UISweepClock::WorkMode::NORMAL); Image *sweepClock1HourImage = new Image(); std::string HourImagePath = std::string(handspath) + "hour.bin"; @@ -497,9 +495,18 @@ namespace OHOS dateLable->SetPosition(date_left, date_top); // widget's left position and top position dateLable->SetViewId("label1"); // Set style dateLable->SetStyle(STYLE_TEXT_FONT, fontsize); - - std::string dateStr = "2024-02-09"; - + /* -------------------------------------------------------------------------- */ + // 将时间字段转换为字符串 + struct tm pTM; + time_t seconds; + time(&seconds); + localtime_r(&seconds, &pTM); + std::string yearStr = std::to_string(pTM.tm_year + 1900); // 年份需要加上 1900 + std::string monthStr = (pTM.tm_mon + 1 < 10) ? "0" + std::to_string(pTM.tm_mon + 1) : std::to_string(pTM.tm_mon + 1); // 月份是从 0 开始的,需要加上 1 + std::string dayStr = (pTM.tm_mday < 10) ? "0" + std::to_string(pTM.tm_mday) : std::to_string(pTM.tm_mday); + // 连接成日期字符串 + std::string dateStr = yearStr + "-" + monthStr + "-" + dayStr; + /* -------------------------------------------------------------------------- */ std::string displayformat = cJSON_GetObjectItem(textdisplay, "displayformat")->valuestring; // 调用日期格式化函数 std::string formattedDate = FormatDate(dateStr, displayformat); @@ -540,12 +547,15 @@ namespace OHOS weekLable->SetViewId("label2"); // Set style weekLable->SetStyle(STYLE_TEXT_FONT, fontsize); - cJSON *weekformat = cJSON_GetObjectItem(displayformat, "weekformat"); std ::string weekStr; if (weekformat != nullptr && cJSON_IsArray(weekformat)) { - cJSON *weekformat1 = cJSON_GetArrayItem(weekformat, 1); + struct tm pTM; + time_t seconds; + time(&seconds); + localtime_r(&seconds, &pTM); + cJSON *weekformat1 = cJSON_GetArrayItem(weekformat, pTM.tm_wday); if (weekformat1 != nullptr && cJSON_IsString(weekformat1)) { weekStr = weekformat1->valuestring; @@ -622,7 +632,6 @@ namespace OHOS batteryLable->SetViewId("label3"); // Set style batteryLable->SetStyle(STYLE_TEXT_FONT, fontsize); - batteryLable->SetText((std::to_string(value) + "%").c_str()); batteryLable->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT); batteryLable->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); @@ -650,8 +659,8 @@ namespace OHOS { changebutton = new UIButton(); } - changebutton->SetPosition(0, 0); - changebutton->Resize(466, 466); + changebutton->SetPosition(0, 0); + changebutton->Resize(466, 466); changebutton->SetViewId(CHANGEBUTTON); changebutton->SetStyle(STYLE_BACKGROUND_COLOR, 0xff000000); changebutton->SetStyle(STYLE_BACKGROUND_OPA, 0x00); @@ -659,7 +668,7 @@ namespace OHOS changebutton->SetStyleForState(STYLE_BACKGROUND_OPA, 0x00, UIButton::PRESSED); changebutton->SetStyleForState(STYLE_BORDER_COLOR, 0xffffffff, UIButton::PRESSED); changebutton->SetStyleForState(STYLE_BORDER_OPA, 0x00, UIButton::PRESSED); - + UIView::OnLongPressListener *buttonLongPress = static_cast(MainPresenterSample::GetInstance()); changebutton->SetOnLongPressListener(buttonLongPress); // Add to rootView -- 2.17.1