优化多国语言变量字符串更新
authorCaisking <3449750381@qq.com>
Thu, 9 May 2024 10:25:45 +0000 (18:25 +0800)
committerCaisking <3449750381@qq.com>
Thu, 9 May 2024 10:25:45 +0000 (18:25 +0800)
code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/src/BloodOxygenView.cpp
code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartView.cpp
code/sdk/application/wearable/fd230app/nativeui/traing/training_card/src/TraingCardPresenter.cpp
code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.cpp
code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.h

index c6bced8..6ee4c6e 100644 (file)
@@ -100,7 +100,7 @@ namespace OHOS
             }
             else if (time_diff <= 60)
             {
-                UpdateLabel(timeLabel, time_diff);
+                UpdateLabel(timeLabel, time_diff,STR_CHECKNO);
             }
             else
             {
index fac88cc..989263e 100644 (file)
@@ -102,7 +102,7 @@ namespace OHOS
             }
             else if (time_diff <= 60)
             {
-                UpdateLabel(timeLabel, time_diff);
+                UpdateLabel(timeLabel, time_diff,STR_CHECKNO);
             }
             else
             {
index 30e2242..86394b3 100644 (file)
@@ -374,7 +374,7 @@ namespace OHOS
     /*训练暂停*/\r
     void TraingCardPresenter::handlePauseState()\r
     {\r
-        TrainingControlView::GetInstance()->GetLabel()->SetText("继续");\r
+        TrainingControlView::GetInstance()->GetLabel()->SetTextId(STR_RESUME);\r
         TrainingControlView::GetInstance()->GetButton()->SetImageSrc(SPORT_CONTINUE_IMAGE, SPORT_CONTINUE_IMAGE);\r
         TraingModel::GetInstance()->SetSportState(ExerciseState::Pause);\r
         PauseTimer();\r
@@ -384,7 +384,7 @@ namespace OHOS
     /*训练继续*/\r
     void TraingCardPresenter::handleContinueState()\r
     {\r
-        TrainingControlView::GetInstance()->GetLabel()->SetText("暂停");\r
+        TrainingControlView::GetInstance()->GetLabel()->SetTextId(STR_PAUSE);\r
         TrainingControlView::GetInstance()->GetButton()->SetImageSrc(SPORT_PAUSE_IMAGE, SPORT_PAUSE_IMAGE);\r
         TraingModel::GetInstance()->SetSportState(ExerciseState::Continue);\r
         ResumeTimer();\r
index c46d586..453879a 100644 (file)
@@ -242,7 +242,7 @@ namespace OHOS
         imageAnimator->SetPosition(left, top);
         imageAnimator->Resize(width, height);
         imageAnimator->SetViewId(viewId);
-        imageAnimator->SetStyle(STYLE_BACKGROUND_COLOR, 0xffffffff);
+        // imageAnimator->SetStyle(STYLE_BACKGROUND_COLOR, 0xffffffff);
         imageAnimator->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
 
         ImageAnimatorInfo *imageAnimatorInfo = new ImageAnimatorInfo[numFrames];
@@ -258,9 +258,9 @@ namespace OHOS
 
         imageAnimator->SetImageAnimatorSrc(imageAnimatorInfo, numFrames, time); // 200 is the default delay time
         imageAnimator->SetTimeOfPause(time);
-        imageAnimator->SetRepeatTimes(1);
-        imageAnimator->SetReverse(true);
-        // imageAnimator->SetVisible(true);
+        imageAnimator->SetRepeat(true);
+        // imageAnimator->SetReverse(true);
+        //  imageAnimator->SetVisible(true);
     }
     /*带默认参数的初始化label*/
     void InitLabelDefault(UILabelExt *&label, int x, int y, int width, int height, const char *viewId, const char *text, int fontSize, const char *fontName)
@@ -269,44 +269,60 @@ namespace OHOS
     }
 
     /*替换字符串中的变量*/
-    void UpdateLabelText(UILabelExt *&label, const std::string &newText)
+    std::string UpdateLabelText(UILabelExt *&label, const std::string &newText, UIResourceTextId id)
     {
         if (label == nullptr)
         {
-            return;
+            return nullptr;
         }
+        /*取Id对应的字符串*/
+        UILabelExt *labelext = new UILabelExt();
+        labelext->SetTextId(id);
+        std::string text = labelext->GetText();
 
-        std::string str = label->GetText();
-        printf("UpdateLabel:%s->%s\r\n", str.c_str(), newText.c_str());
+        printf("UpdateLabel:%s->%s\r\n", text.c_str(), newText.c_str());
 
         // 找到标志"XX"在字符串中的位置
-        size_t pos = str.find("XX");
+        size_t pos = text.find("XX");
         if (pos != std::string::npos)
         {
             // 替换标志"XX"为更新字符串
-            str.replace(pos, 2, newText); // 2 是标志"XX"的长度
-            label->SetText(str.c_str());
+            text.replace(pos, 2, newText); // 2 是标志"XX"的长度
+            printf("newText:%s\r\n", text.c_str());
+            label->SetText(text.c_str());
+        }
+        else
+        {
+            printf("找不到变量标志位XX,请注意是否已经被修改!\r\n");
         }
+
+        delete labelext;
+        return text;
     }
     /*更新数字*/
-    void UpdateLabel(UILabelExt *&label, int num)
+    std::string UpdateLabel(UILabelExt *&label, int num, UIResourceTextId id)
     {
+        if (label == nullptr)
         {
-            return;
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "label is error");
+            return nullptr;
         }
 
-        UpdateLabelText(label, std::to_string(num));
+        std::string str = UpdateLabelText(label, std::to_string(num), id);
+        return str;
     }
 
     /*更新文本*/
-    void UpdateLabel(UILabelExt *&label, const std::string &updateStr)
+    std::string UpdateLabel(UILabelExt *&label, const std::string &updateStr, UIResourceTextId id)
     {
         if (label == nullptr)
         {
-            return;
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "label is error");
+            return nullptr;
         }
 
-        UpdateLabelText(label, updateStr);
+        std::string str = UpdateLabelText(label, updateStr, id);
+        return str;
     }
 
     /*设置label的Id*/
@@ -332,4 +348,3 @@ namespace OHOS
 
 }
 
-/*更新label的字符串*/
index 88027cf..ba19be1 100644 (file)
@@ -53,11 +53,11 @@ namespace OHOS
     void InitLabelDefault(UILabelExt *&label, int x, int y, int width, int height, const char *viewId, const char *text, int fontSize = 30, const char *fontName = DEFAULT_VECTOR_FONT_FILENAME);
 
     /*通用替换*/
-    void UpdateLabelText(UILabelExt *&label, const std::string &newText);
+    std::string UpdateLabelText(UILabelExt *&label, const std::string &newText, UIResourceTextId id);
     /*更新label的数字*/
-    void UpdateLabel(UILabelExt *&label, int num);
+    std::string UpdateLabel(UILabelExt *&label, int num, UIResourceTextId id);
     /*更新label的字符串*/
-    void UpdateLabel(UILabelExt *&label, const std::string &updatestr);
+    std::string UpdateLabel(UILabelExt *&label, const std::string &updatestr, UIResourceTextId id);
     /*通过labelext获取Id设置label*/
     void SetLabelId(UILabel *&label, UIResourceTextId id);
 }