From: 罗正yun <1732036298@qq.com> Date: Sat, 20 Jan 2024 03:24:18 +0000 (+0800) Subject: 按键返回键配置,设置视图的返回键无效 X-Git-Tag: release_V1.2.70_20241020_135402~1351^2~5^2~1 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=fb812555e8419f64054441849835f9b51fbf70c7;p=mirrors%2FFD230.git 按键返回键配置,设置视图的返回键无效 --- diff --git a/code/sdk/application/wearable/nativeapp/nativeui/compass/include/MainCompassPresenter.h b/code/sdk/application/wearable/nativeapp/nativeui/compass/include/MainCompassPresenter.h index 30580afcd..b3597629b 100755 --- a/code/sdk/application/wearable/nativeapp/nativeui/compass/include/MainCompassPresenter.h +++ b/code/sdk/application/wearable/nativeapp/nativeui/compass/include/MainCompassPresenter.h @@ -16,10 +16,11 @@ #include "CompassPresenter.h" #include "CompassModel.h" #include "CompassCalibrationView.h" +#include "KeyInputListener.h" namespace OHOS { class CompassView; -class MainCompassPresenter : public Presenter, public CompassPresenter, +class MainCompassPresenter : public Presenter, public CompassPresenter, public RootView::OnKeyActListener, public ModelListenerSample, public UIView::OnDragListener { public: MainCompassPresenter(); @@ -32,6 +33,7 @@ public: static MainCompassPresenter *GetInstance(void); bool OnDrag(UIView& view, const DragEvent& event) override; + bool OnKeyAct(UIView &view, const KeyEvent &event) override; // 增加按键事件 virtual void Notify() override; }; } diff --git a/code/sdk/application/wearable/nativeapp/nativeui/compass/src/MainCompassPresenter.cpp b/code/sdk/application/wearable/nativeapp/nativeui/compass/src/MainCompassPresenter.cpp index 11787e542..0e6d57d7d 100755 --- a/code/sdk/application/wearable/nativeapp/nativeui/compass/src/MainCompassPresenter.cpp +++ b/code/sdk/application/wearable/nativeapp/nativeui/compass/src/MainCompassPresenter.cpp @@ -8,7 +8,7 @@ #include "MainCompassPresenter.h" #include "MainCompassView.h" #include "wearable_log.h" - +#include "common/key_code.h" namespace OHOS { static MainCompassPresenter *g_pMainView = nullptr; @@ -48,6 +48,19 @@ bool MainCompassPresenter::OnDrag(UIView& view, const DragEvent& event) return true; } +// 增加按键事件 +bool MainCompassPresenter::OnKeyAct(UIView &view, const KeyEvent &event) +{ + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainCompassPresenter::OnKeyAct"); + uint16 keyID = event.GetKeyId(); + //如果是power键就返回上一级 + if (keyID == static_cast(ZliteKeyCode::ZLITE_KEY_POWER)) + { + uint16_t preSlice = NativeAbility::GetInstance().GetPreSliceId(); + NativeAbility::GetInstance().ChangeSlice(preSlice); + } + return true; +} void MainCompassPresenter::Notify() { diff --git a/code/sdk/application/wearable/nativeapp/nativeui/compass/src/MainCompassView.cpp b/code/sdk/application/wearable/nativeapp/nativeui/compass/src/MainCompassView.cpp index 9e762804a..e240a47ce 100755 --- a/code/sdk/application/wearable/nativeapp/nativeui/compass/src/MainCompassView.cpp +++ b/code/sdk/application/wearable/nativeapp/nativeui/compass/src/MainCompassView.cpp @@ -106,6 +106,10 @@ void MainCompassView::OnStart() compassCalibrationView->SetVisible(false); } CompassModel::GetInstance()->OpenCompassSensor(); + + // 增加按键监听 + RootView::OnKeyActListener *keyactListener = (RootView::OnKeyActListener *)OHOS::MainCompassPresenter::GetInstance(); + RootView::GetInstance()->SetOnKeyActListener(keyactListener); } void MainCompassView::OnSetUpView() diff --git a/code/sdk/application/wearable/nativeapp/nativeui/settings/include/SettingPresenter.h b/code/sdk/application/wearable/nativeapp/nativeui/settings/include/SettingPresenter.h index f6cd58742..6fd77eefd 100755 --- a/code/sdk/application/wearable/nativeapp/nativeui/settings/include/SettingPresenter.h +++ b/code/sdk/application/wearable/nativeapp/nativeui/settings/include/SettingPresenter.h @@ -11,7 +11,6 @@ #include "Presenter.h" #include "components/ui_view.h" #include "KeyInputListener.h" -#include "common/key_code.h" namespace OHOS { diff --git a/code/sdk/application/wearable/nativeapp/nativeui/settings/src/SettingPresenter.cpp b/code/sdk/application/wearable/nativeapp/nativeui/settings/src/SettingPresenter.cpp index ee7b19625..c14e23918 100755 --- a/code/sdk/application/wearable/nativeapp/nativeui/settings/src/SettingPresenter.cpp +++ b/code/sdk/application/wearable/nativeapp/nativeui/settings/src/SettingPresenter.cpp @@ -9,6 +9,7 @@ #include "SettingModel.h" #include "SettingPresenter.h" #include "RebootModel.h" +#include "common/key_code.h" namespace OHOS { @@ -56,26 +57,33 @@ namespace OHOS (event.GetDeltaX() > X_DRAG_OFFSET) && (event.GetDeltaX() > (DOUBLES * MATH_ABS(event.GetDeltaY())))) { WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SettingPresenter::OnDrag ChangeSlice(VIEW_APPLIST)"); - NativeAbility::GetInstance().ChangeSlice(VIEW_APPLIST); + /* + GetPreSliceId作用:是从已存储的切片中获取上一个切片的ID,并且在切片数小于2的情况下返回一个无效的切片ID。 + */ + uint16_t preSlice = NativeAbility::GetInstance().GetPreSliceId(); + if(preSlice == VIEW_APPLIST){ + NativeAbility::GetInstance().ChangeSlice(preSlice); + } + else + { + NativeAbility::GetInstance().ChangeSlice(VIEW_MAIN_SAMPLE); + } + } return false; } // 增加按键事件 bool SettingPresenter::OnKeyAct(UIView &view, const KeyEvent &event) { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SettingPresenter::OnKeyAct"); uint16 keyID = event.GetKeyId(); - WEARABLE_LOGD("", "into key! keyID %u\n", keyID); -#if 0 + printf("%d %s %d\n",keyID,__FUNCTION__,__LINE__); //如果是power键就返回上一级 if (keyID == static_cast(ZliteKeyCode::ZLITE_KEY_POWER)) { uint16_t preSlice = NativeAbility::GetInstance().GetPreSliceId(); - NativeAbility::GetInstance().ChangeSlice(VIEW_APPLIST); + NativeAbility::GetInstance().ChangeSlice(preSlice); } -#endif - // 目前只有一个按键。直接切换 - uint16_t preSlice = NativeAbility::GetInstance().GetPreSliceId(); - NativeAbility::GetInstance().ChangeSlice(preSlice); return true; } diff --git a/code/sdk/application/wearable/nativeapp/nativeui/settings/src/SettingView.cpp b/code/sdk/application/wearable/nativeapp/nativeui/settings/src/SettingView.cpp index 922b31ddb..6f5ca5ce8 100755 --- a/code/sdk/application/wearable/nativeapp/nativeui/settings/src/SettingView.cpp +++ b/code/sdk/application/wearable/nativeapp/nativeui/settings/src/SettingView.cpp @@ -138,6 +138,7 @@ namespace OHOS scrollView->Add(title); scrollView->Add(contentList); RootView::GetInstance()->Add(scrollView); + contentList->SetAdapter(listAdapter); Draw(); @@ -155,12 +156,6 @@ namespace OHOS { WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SettingView::OnStop"); View::OnStop(); - if (container_ != nullptr) - { - container_->RemoveAll(); - delete container_; - container_ = nullptr; - } RootView::GetInstance()->ClearOnKeyActListener(); RootView::GetInstance()->RemoveAll(); return; diff --git a/code/sdk/application/wearable/nativeapp/nativeui/weather/include/MainWeatherPresenter.h b/code/sdk/application/wearable/nativeapp/nativeui/weather/include/MainWeatherPresenter.h index 70bbd4ec2..0ad0ca063 100644 --- a/code/sdk/application/wearable/nativeapp/nativeui/weather/include/MainWeatherPresenter.h +++ b/code/sdk/application/wearable/nativeapp/nativeui/weather/include/MainWeatherPresenter.h @@ -3,14 +3,14 @@ #include "Presenter.h" #include "MainWeatherView.h" - +#include "KeyInputListener.h" /*主天气视图*/ namespace OHOS { class MainWeatherView; - class MainWeatherPresenter : public Presenter, public UIView::OnClickListener, + public RootView::OnKeyActListener, public UIView::OnDragListener { public: @@ -28,6 +28,7 @@ namespace OHOS // 因为继承了OnClickListener,所以有OnClick属性,至于参数,原函数也这样直接继承; bool OnClick(UIView &view, const ClickEvent &event) override; bool OnDrag(UIView &view, const DragEvent &event) override; + bool OnKeyAct(UIView &view, const KeyEvent &event) override; // 增加按键事件 }; } // namespace OHOS diff --git a/code/sdk/application/wearable/nativeapp/nativeui/weather/include/MainWeatherView.h b/code/sdk/application/wearable/nativeapp/nativeui/weather/include/MainWeatherView.h index 37bd30567..15b801615 100644 --- a/code/sdk/application/wearable/nativeapp/nativeui/weather/include/MainWeatherView.h +++ b/code/sdk/application/wearable/nativeapp/nativeui/weather/include/MainWeatherView.h @@ -55,7 +55,7 @@ namespace OHOS UILabel *label1_ = nullptr; UILabel *label2_ = nullptr; UIImageView *image1_ = nullptr; - UILabel *label3_ = nullptr; + UILabel *showWeatherDescriptionLabel = nullptr; UILabel *label4_ = nullptr; UILabel *label5_ = nullptr; UIImageAnimatorView *imageAnimator1_ = nullptr; diff --git a/code/sdk/application/wearable/nativeapp/nativeui/weather/src/MainWeatherPresenter.cpp b/code/sdk/application/wearable/nativeapp/nativeui/weather/src/MainWeatherPresenter.cpp index d065faf1f..6acebea7a 100644 --- a/code/sdk/application/wearable/nativeapp/nativeui/weather/src/MainWeatherPresenter.cpp +++ b/code/sdk/application/wearable/nativeapp/nativeui/weather/src/MainWeatherPresenter.cpp @@ -1,7 +1,7 @@ #include "AppViewIDs.h" #include "NativeAbility.h" #include "weather/include/MainWeatherPresenter.h" - +#include "common/key_code.h" namespace OHOS { @@ -58,11 +58,11 @@ namespace OHOS NativeAbility::GetInstance().ChangeSlice(VIEW_APPLIST); return true; } - else if (event.GetDragDirection() == DragEvent::DIRECTION_BOTTOM_TO_TOP) - { - NativeAbility::GetInstance().ChangeSlice(VIEW_WEATHER_REPORT); // 主天气界面从下往上滑进入气象报告界面 - return true; - } + // else if (event.GetDragDirection() == DragEvent::DIRECTION_BOTTOM_TO_TOP) + // { + // NativeAbility::GetInstance().ChangeSlice(VIEW_WEATHER_REPORT); // 主天气界面从下往上滑进入气象报告界面 + // return true; + // } return false; } @@ -73,4 +73,18 @@ namespace OHOS // if (strcmp(view.GetViewId(), 控件ID) == 0) return true; } + + // 增加按键事件 + bool MainWeatherPresenter::OnKeyAct(UIView &view, const KeyEvent &event) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SettingPresenter::OnKeyAct"); + uint16 keyID = event.GetKeyId(); + //如果是power键就返回上一级 + if (keyID == static_cast(ZliteKeyCode::ZLITE_KEY_POWER)) + { + uint16_t preSlice = NativeAbility::GetInstance().GetPreSliceId(); + NativeAbility::GetInstance().ChangeSlice(preSlice); + } + return true; + } } \ No newline at end of file diff --git a/code/sdk/application/wearable/nativeapp/nativeui/weather/src/MainWeatherView.cpp b/code/sdk/application/wearable/nativeapp/nativeui/weather/src/MainWeatherView.cpp index bf37ce375..f7890a89c 100644 --- a/code/sdk/application/wearable/nativeapp/nativeui/weather/src/MainWeatherView.cpp +++ b/code/sdk/application/wearable/nativeapp/nativeui/weather/src/MainWeatherView.cpp @@ -151,8 +151,21 @@ namespace OHOS label2_ = InitLabel(249, 126, Temperature.c_str(), "label2", 100, 125); scrollView1_->Add(label2_); /* -------------------------------------------------------------------------- */ - label3_ = InitLabel(111, 264, WeatherDescription.c_str(), "label3", 35, 41); - scrollView1_->Add(label3_); + showWeatherDescriptionLabel = new UILabel(); + if (showWeatherDescriptionLabel == nullptr) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::new showWeatherDescriptionLabel fail!"); + return ; + } + showWeatherDescriptionLabel->SetPosition(83, 264,300,40); + showWeatherDescriptionLabel->SetViewId("showWeatherDescriptionLabel"); + showWeatherDescriptionLabel->SetStyle(STYLE_TEXT_FONT, 35); + showWeatherDescriptionLabel->SetStyle(STYLE_LINE_HEIGHT, 41); + showWeatherDescriptionLabel->SetText(WeatherDescription.c_str()); + showWeatherDescriptionLabel->SetLineBreakMode(UILabel::LINE_BREAK_WRAP); + showWeatherDescriptionLabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + showWeatherDescriptionLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 35); + scrollView1_->Add(showWeatherDescriptionLabel); /* -------------------------------------------------------------------------- */ label4_ = InitLabel(155, 330, WeatherArea.c_str(), "label4", 26, 30); label4_->SetTextColor(Color::GetColorFromRGBA(0x99, 0x99, 0x99, 0xff)); @@ -205,10 +218,10 @@ namespace OHOS delete image1_; image1_ = nullptr; } - if (label3_ != nullptr) + if (showWeatherDescriptionLabel != nullptr) { - delete label3_; - label3_ = nullptr; + delete showWeatherDescriptionLabel; + showWeatherDescriptionLabel = nullptr; } if (label4_ != nullptr) { @@ -260,6 +273,9 @@ namespace OHOS } PreLoad(); RootView::GetInstance()->Add(container_); + // 增加按键监听 + RootView::OnKeyActListener *keyactListener = (RootView::OnKeyActListener *)OHOS::MainWeatherPresenter::GetInstance(); + RootView::GetInstance()->SetOnKeyActListener(keyactListener); } void MainWeatherView::OnSetUpView() @@ -277,8 +293,8 @@ namespace OHOS void MainWeatherView::OnStop() { - View::OnStop(); + RootView::GetInstance()->ClearOnKeyActListener(); RootView::GetInstance()->RemoveAll(); return; } diff --git a/code/sdk/application/wearable/nativeapp/nativeui/weather/src/WeatherView.cpp b/code/sdk/application/wearable/nativeapp/nativeui/weather/src/WeatherView.cpp index 273ba54de..12922aa31 100644 --- a/code/sdk/application/wearable/nativeapp/nativeui/weather/src/WeatherView.cpp +++ b/code/sdk/application/wearable/nativeapp/nativeui/weather/src/WeatherView.cpp @@ -179,7 +179,7 @@ namespace OHOS return false; } /* -------------------------------------------------------------------------- */ - UILabel *showWeatherDescriptionLabel = new UILabel(); + showWeatherDescriptionLabel = new UILabel(); showWeatherDescriptionLabel->SetPosition(83, 264,300,40); showWeatherDescriptionLabel->SetViewId("showWeatherDescriptionLabel"); showWeatherDescriptionLabel->SetStyle(STYLE_TEXT_FONT, 35);