优化GPS相关提醒的弹窗退出
authorCaisking <Casiking@zhihuishixun.com>
Sat, 24 Jan 2026 11:18:11 +0000 (19:18 +0800)
committerCaisking <Casiking@zhihuishixun.com>
Sat, 24 Jan 2026 11:18:11 +0000 (19:18 +0800)
code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/training_GPS/include/TraingEphemerisNotePresenter.h
code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/training_GPS/include/TraingGPSnotePresenter.h
code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/training_GPS/include/TraingGPSnoteView.h
code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/training_GPS/src/TraingEphemerisNotePresenter.cpp
code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/training_GPS/src/TraingEphemerisNoteView.cpp
code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/training_GPS/src/TraingGPSnotePresenter.cpp
code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/training_GPS/src/TraingGPSnoteView.cpp

index d172b88..54ca223 100644 (file)
@@ -4,6 +4,8 @@
 #include "key/include/KeyInputListener.h"
 #include "Presenter.h"
 #include "TraingEphemerisNoteView.h"
+#include "components/root_view.h"
+#include "events/key_event.h"
 
 namespace OHOS
 {
@@ -11,7 +13,8 @@ namespace OHOS
 
     class TraingEphemerisNotePresenter : public Presenter<TraingEphemerisNoteView>,
                                  public UIView::OnClickListener,
-                                 public UIView::OnDragListener
+                                 public UIView::OnDragListener,
+                                 public RootView::OnKeyActListener
     {
     public:
         TraingEphemerisNotePresenter();
@@ -27,6 +30,8 @@ namespace OHOS
         bool OnClick(UIView &view, const ClickEvent &event) override;
         bool OnDrag(UIView &view, const DragEvent &event) override;
         bool OnDragEnd(UIView& view, const DragEvent& event) override;
+        bool OnKeyAct(UIView &view, const KeyEvent &event) override; // 增加按键事件
+        bool X_DRAG_FLAG{false};
     };
 } // namespace OHOS
 
index 98fd935..45a050f 100644 (file)
@@ -4,6 +4,8 @@
 #include "key/include/KeyInputListener.h"
 #include "Presenter.h"
 #include "TraingGPSnoteView.h"
+#include "components/root_view.h"
+#include "events/key_event.h"
 
 namespace OHOS
 {
@@ -11,7 +13,8 @@ namespace OHOS
 
     class TraingGPSnotePresenter : public Presenter<TraingGPSnoteView>,
                                  public UIView::OnClickListener,
-                                 public UIView::OnDragListener
+                                 public UIView::OnDragListener,
+                                 public RootView::OnKeyActListener
     {
     public:
         TraingGPSnotePresenter();
@@ -27,6 +30,8 @@ namespace OHOS
         bool OnClick(UIView &view, const ClickEvent &event) override;
         bool OnDrag(UIView &view, const DragEvent &event) override;
         bool OnDragEnd(UIView& view, const DragEvent& event) override;
+        bool OnKeyAct(UIView &view, const KeyEvent &event) override; // 增加按键事件
+        bool X_DRAG_FLAG{false};
     };
 } // namespace OHOS
 
index c24c902..6e423c5 100644 (file)
@@ -25,8 +25,8 @@
 
 #include "TraingGPSnotePresenter.h"
 namespace OHOS {
-#define BUTTONBACK "button1"
-#define BUTTONGO "button2"
+#define GPSBUTTONBACK "button_back_gps"
+#define GPSBUTTONGO "button_go_gps"
 
 class TraingGPSnotePresenter;
 class TraingGPSnoteView : public View<TraingGPSnotePresenter> {
index 2678c38..8be437c 100644 (file)
@@ -12,6 +12,8 @@
 #include "traing/include/TraingModel.h"
 #include "traing/include/TraingNomalPresenter.h"
 
+
+
 #include "NativeRegisterManager.h"
 namespace OHOS
 {
@@ -61,6 +63,7 @@ namespace OHOS
 
     bool TraingEphemerisNotePresenter::OnClick(UIView &view, const ClickEvent &event)
     {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "OnClick view.GetViewId() is %s", view.GetViewId());
         // 根据控件id查找是哪个控件触发了点击回调
         if (strcmp(view.GetViewId(), BUTTONTRAIN) == 0)
         {
@@ -81,5 +84,22 @@ namespace OHOS
         }
         return true;
     }
+
+    //增加按键退出
+    bool TraingEphemerisNotePresenter::OnKeyAct(UIView &view, const KeyEvent &event)
+    {
+        unsigned short keyID = event.GetKeyId();
+        unsigned short keyState = event.GetState();
+        
+        // 按键按下时退出到训练主界面
+        if (keyState == 1) // 1表示按下
+        {
+            NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGNOMAL);
+            return true;
+        }
+        UNUSED(view);
+        UNUSED(keyID);
+        return true;
+    }
 } // namespace OHOS
 
index c30e299..d185088 100644 (file)
 // #include "TraingGPSnoteView.h"
 
 #include "NativeRegisterManager.h"
+#include "UiConfig.h"
+#include "gfx_utils/graphic_math.h"
+#include "custom_runtime.h"
+#include "custom_utils.h"
+#include "common/key_code.h"
+#include "components/root_view.h"
 namespace OHOS
 {
     REGIST_SLICE(VIEW_TRAINING_GPSNOTE, TraingGPSnoteView, TraingGPSnotePresenter);
@@ -44,39 +50,71 @@ namespace OHOS
 
     void TraingGPSnotePresenter::OnResume(void)
     {
+        RootView::GetInstance()->SetOnKeyActListener(this);
     }
     
     bool TraingGPSnotePresenter::OnDrag(UIView &view, const DragEvent &event)
     {
+        UNUSED(view);
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "TraingGPSnotePresenter::OnDrag");
+        if ((event.GetDeltaX() > X_DRAG_OFFSET) && (event.GetDeltaX() > (DOUBLES * MATH_ABS(event.GetDeltaY()))))
+        {
+            X_DRAG_FLAG = true;
+        }
         return true;
     }
     bool TraingGPSnotePresenter::OnDragEnd(UIView &view, const DragEvent &event)
     {
-
-        if (event.GetDragDirection() == DragEvent::DIRECTION_LEFT_TO_RIGHT)
+        UNUSED(view);
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "TraingGPSnotePresenter::OnDragEnd");
+        if (event.GetDragDirection() == DragEvent::DIRECTION_LEFT_TO_RIGHT && X_DRAG_FLAG)
         {
             uint16_t preSlice = NativeAbility::GetInstance().GetPreSliceId();
             NativeAbility::GetInstance().ChangeSlice(preSlice);
+            X_DRAG_FLAG = false;
         }
-        UNUSED(view);
         return true;
     }
 
     bool TraingGPSnotePresenter::OnClick(UIView &view, const ClickEvent &event)
     {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "TraingGPSnotePresenter::OnClick %s", view.GetViewId());
         // 根据控件id查找是哪个控件触发了点击回调
-        if (strcmp(view.GetViewId(), BUTTONBACK) == 0)
+        if (strcmp(view.GetViewId(), GPSBUTTONBACK) == 0)
         {
-            NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGNOMAL);
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "TraingGPSnotePresenter::OnClick BUTTONBACK");
+            uint16_t preSlice = NativeAbility::GetInstance().GetPreSliceId();
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "TraingGPSnotePresenter::OnClick preSlice is %d", preSlice);
+            NativeAbility::GetInstance().ChangeSlice(preSlice);
+            return true;
         }
-        if (strcmp(view.GetViewId(), BUTTONGO) == 0)
+        if (strcmp(view.GetViewId(), GPSBUTTONGO) == 0)
         {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "TraingGPSnotePresenter::OnClick BUTTONGO");
             NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGCRAD);
-
             // 启动运动相关资源
             ExerciseClass type = TraingModel::GetInstance()->GetSportId();
             TraingCardPresenter::GetInstance()->OpenSensor(type);
             TraingModel::GetInstance()->SetSportOpenWay(false);
+            return true;
+        }
+        return false;
+    }
+
+    //增加按键退出
+    bool TraingGPSnotePresenter::OnKeyAct(UIView &view, const KeyEvent &event)
+    {
+        UNUSED(view);
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "TraingGPSnotePresenter::OnKeyAct");
+        
+        
+        uint16_t keyID = event.GetKeyId();
+        uint16_t keyState = event.GetState();
+        // 如果是power键,且是亮屏状态,按键释放就返回上一级
+        if (keyID == static_cast<uint16_t>(ZliteKeyCode::ZLITE_KEY_POWER) && keyState == 1 && uapi_get_screen_state() == 0)
+        {
+            uint16_t preSlice = NativeAbility::GetInstance().GetPreSliceId();
+            NativeAbility::GetInstance().ChangeSlice(preSlice);
         }
         return true;
     }
index 8583e09..29840d4 100644 (file)
@@ -12,6 +12,7 @@
 #include "uicomponents/UIComponents.h"
 #include "traing/Training_UiConfig.h"
 #include "ui_resource_image.h"
+#include "components/root_view.h"
 namespace OHOS
 {
 
@@ -51,6 +52,7 @@ namespace OHOS
         scrollView1_->SetReboundSize(100);
         scrollView1_->SetScrollBlankSize(52);
         scrollView1_->SetHorizontalScrollState(false);
+        scrollView1_->SetOnDragListener(presenter_);
         // Add to rootView
         AddViewToRootContainer(scrollView1_);
         scrollView1_->SetVisible(true);
@@ -60,13 +62,13 @@ namespace OHOS
         scrollView1_->Add(image1_);
 
         // 初始化button
-        InitButtonExt(button_wait, 118, 334, 230, 80, BUTTONBACK, STR_SET_TARGET, 30);
+        InitButtonExt(button_wait, 118, 334, 230, 80, GPSBUTTONBACK, STR_SET_TARGET, 30);
         button_wait->SetText("继续等待");
         button_wait->SetStyle(STYLE_BACKGROUND_COLOR, 0xff4fc34c);
         scrollView1_->Add(button_wait);
         button_wait->SetOnClickListener(presenter_);
 
-        InitButtonExt(button_go, 118, 434, 230, 80, BUTTONGO, STR_SET_TARGET, 30);
+        InitButtonExt(button_go, 118, 434, 230, 80, GPSBUTTONGO, STR_SET_TARGET, 30);
         button_go->SetText("直接开始");
         scrollView1_->Add(button_go);
         button_go->SetOnClickListener(presenter_);
@@ -148,18 +150,10 @@ namespace OHOS
     {
 
         // 创建 draglistener 实例并设置为拖动监听器
-        UIView::OnDragListener *draglistener = static_cast<UIView::OnDragListener *>(TraingGPSnotePresenter::GetInstance());
-        if (draglistener == nullptr)
-        {
-            return;
-        }
-        GetRootContainer()->SetOnDragListener(draglistener);
-
-        GetRootContainer()->SetTouchable(true);
-        GetRootContainer()->SetDraggable(true);
 
         PreLoad();
-
+        // 注册按键监听
+        RootView::GetInstance()->SetOnKeyActListener(presenter_);
     }
 
 }