From: 罗正yun <1732036298@qq.com> Date: Sat, 20 Jan 2024 10:06:23 +0000 (+0800) Subject: 1.修改设置视图按键事件,现在可通过power键进行返回。 X-Git-Tag: release_V1.2.70_20241020_135402~1351^2^2 X-Git-Url: http://git.snicker.com.cn/Users/apple/Desktop/static/gitweb.css?a=commitdiff_plain;h=7517f529c1d01c91e9f7545adeefb11faff4391f;p=mirrors%2FFD230.git 1.修改设置视图按键事件,现在可通过power键进行返回。 2.但又出现新的问题:设置中的蓝牙也有按键事件,如果我进入蓝牙slice,通过按power键返回时按键事件触发了两次。 解决方法:1.标志位来跟踪按键事件的处理状态(测试了不行) 2.网上查阅资料说:可使用观察者模式来解决这个问题, --- diff --git a/code/sdk/application/wearable/nativeapp/nativeui/bluetooth/src/BluetoothPresenter.cpp b/code/sdk/application/wearable/nativeapp/nativeui/bluetooth/src/BluetoothPresenter.cpp index 7cd3fc91c..cd0ee58df 100755 --- a/code/sdk/application/wearable/nativeapp/nativeui/bluetooth/src/BluetoothPresenter.cpp +++ b/code/sdk/application/wearable/nativeapp/nativeui/bluetooth/src/BluetoothPresenter.cpp @@ -222,17 +222,12 @@ bool BluetoothPresenter::OnClick(UIView &view, const ClickEvent &event) { uint16 keyID = event.GetKeyId(); WEARABLE_LOGD("","into key! keyID %u\n",keyID); - #if 0 //如果是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/SettingPresenter.cpp b/code/sdk/application/wearable/nativeapp/nativeui/settings/src/SettingPresenter.cpp index df9d858f2..73d5ad2c6 100755 --- a/code/sdk/application/wearable/nativeapp/nativeui/settings/src/SettingPresenter.cpp +++ b/code/sdk/application/wearable/nativeapp/nativeui/settings/src/SettingPresenter.cpp @@ -16,11 +16,16 @@ namespace OHOS static SettingPresenter *g_pSettingPresenter = nullptr; SettingPresenter::SettingPresenter() { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SettingPresenter::SettingPresenter()"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SettingPresenter::SettingPresenter"); settingModel = &SettingModel::GetInstance(); + g_pSettingPresenter = this; } - SettingPresenter::~SettingPresenter() {} + SettingPresenter::~SettingPresenter() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SettingPresenter::~SettingPresenter"); + g_pSettingPresenter = nullptr; + } void SettingPresenter::OnStart() { @@ -61,14 +66,14 @@ namespace OHOS GetPreSliceId作用:是从已存储的切片中获取上一个切片的ID,并且在切片数小于2的情况下返回一个无效的切片ID。 */ uint16_t preSlice = NativeAbility::GetInstance().GetPreSliceId(); - if(preSlice == VIEW_APPLIST){ + if (preSlice == VIEW_APPLIST) + { NativeAbility::GetInstance().ChangeSlice(preSlice); } - else + else { NativeAbility::GetInstance().ChangeSlice(VIEW_MAIN); } - } return false; } @@ -77,13 +82,19 @@ namespace OHOS { WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SettingPresenter::OnKeyAct"); uint16 keyID = event.GetKeyId(); - 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(preSlice); - } + // 如果是power键就返回上一级 + if (keyID == static_cast(ZliteKeyCode::ZLITE_KEY_POWER)) + { + uint16_t preSlice = NativeAbility::GetInstance().GetPreSliceId(); + if (preSlice == VIEW_APPLIST) + { + NativeAbility::GetInstance().ChangeSlice(preSlice); + } + else + { + NativeAbility::GetInstance().ChangeSlice(VIEW_MAIN); + } + } return true; } @@ -100,8 +111,8 @@ namespace OHOS else { NativeAbility::GetInstance().ChangeSlice(SettingModel::GetInstance().GetSettingIdToViewId().at(index)); - /* -------------------------------------------------------------------------- */ - #if 0 +/* -------------------------------------------------------------------------- */ +#if 0 if (index == SETTING_ITEM_TWO) { RebootModel::GetInstance()->SetSelectViewId(REBOOTDEVICE); @@ -113,7 +124,7 @@ namespace OHOS RebootModel::GetInstance()->SetExitsView(true); } NativeAbility::GetInstance().ChangeSlice(SettingModel::GetInstance().GetSettingIdToViewId().at(index)); - #endif +#endif return true; } } 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 15b801615..b5554d403 100644 --- a/code/sdk/application/wearable/nativeapp/nativeui/weather/include/MainWeatherView.h +++ b/code/sdk/application/wearable/nativeapp/nativeui/weather/include/MainWeatherView.h @@ -3,15 +3,7 @@ #include "BaseView.h" #include "View.h" - -#include "components/ui_view_group.h" -#include "components/ui_scroll_view.h" -#include "components/ui_label.h" -#include "components/ui_image_view.h" -#include "components/ui_image_animator.h" #include "weather/include/WeatherView.h" -#include - namespace OHOS { @@ -30,36 +22,7 @@ namespace OHOS void OnStop() override; private: - void InitView(void); - void DeInitView(void); - UILabel *InitLabel(int x, int y, const char *text, const char *id, int textfont, int lineheight); - void PreLoad(void); - - private: - std::string GetTime(); - std::string GetWeatherImage(); - std::string GetWeatherTemperature(); - std::string GetWeatherDescription(); - std::string GetWeatherArea(); - std::string GetWeatherMaxAndMinTemperature(); - - std::string Time; // 时间 - std::string WeatherImag; // 天气图片 - std::string Temperature; // 温度 - std::string WeatherDescription; // 天气描述 - std::string WeatherArea; // 天气地区 - std::string MaxandMinTemperature; // 最高与最低气温 - private: - UIViewGroup *container_ = nullptr; - UIScrollView *scrollView1_ = nullptr; - UILabel *label1_ = nullptr; - UILabel *label2_ = nullptr; - UIImageView *image1_ = nullptr; - UILabel *showWeatherDescriptionLabel = nullptr; - UILabel *label4_ = nullptr; - UILabel *label5_ = nullptr; - UIImageAnimatorView *imageAnimator1_ = nullptr; - bool viewInitStatus{false}; + WeatherView * weatherView{nullptr}; }; } // namespace OHOS #endif \ 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 f7890a89c..8a7ceb3f5 100644 --- a/code/sdk/application/wearable/nativeapp/nativeui/weather/src/MainWeatherView.cpp +++ b/code/sdk/application/wearable/nativeapp/nativeui/weather/src/MainWeatherView.cpp @@ -1,278 +1,40 @@ #include "weather/include/MainWeatherView.h" #include "weather/include/MainWeatherPresenter.h" -#include "UiConfig.h" -/* -当至天气界面时,主动获取最新天气数据。 -(此操作静默将进行,获取到则立刻更新显示,获取不到则重试 -重试3次无果结束更新) -在加载该视图时,需要加载:1时间,2天气图片,3温度,4天气描述,5,地点,6,温度范围 -*/ - namespace OHOS { - static ImageAnimatorInfo g_imageAnimator1Info[2] = { - {PNG_ANIMATOR1_IMAGE, {213, 400}, 40, 40, IMG_SRC_FILE_PATH}, - {PNG_ANIMATOR2_IMAGE, {213, 400}, 40, 40, IMG_SRC_FILE_PATH}, - }; static MainWeatherView *g_pMainWeatherView{nullptr}; - MainWeatherView::MainWeatherView() { - Time = GetTime(); - WeatherImag = GetWeatherImage(); - Temperature = GetWeatherTemperature(); - WeatherDescription = GetWeatherDescription(); - WeatherArea = GetWeatherArea(); - MaxandMinTemperature = GetWeatherMaxAndMinTemperature(); g_pMainWeatherView = this; } MainWeatherView::~MainWeatherView() { - if (viewInitStatus == true) - { - DeInitView(); - } + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainWeatherView::~MainWeatherView"); + RootView::GetInstance()->Remove(weatherView); + delete weatherView; + weatherView = nullptr; g_pMainWeatherView = nullptr; } - MainWeatherView *MainWeatherView::GetInstance() - { - return g_pMainWeatherView; - } - - // 获取时间 - std::string MainWeatherView::GetTime() - { - srand(time(0)); - std::string area[] = {"23:54", "12:00", "7:25"}; - int num = rand() % (sizeof(area) / sizeof(area[0])); - return area[1]; - } - - // 获取天气图片 - std::string MainWeatherView::GetWeatherImage() - { - int size = sizeof(weather130x130) / sizeof(weather130x130[0]); - return weather130x130[8]; - } - // 获取温度 - - std::string MainWeatherView::GetWeatherTemperature() - { - // 使用系统时间作为种子 - srand(time(0)); - int temperature = rand() % 50; - return std::to_string(22) + "°"; - } - // 获取天气描述 - - std::string MainWeatherView::GetWeatherDescription() - { - srand(time(0)); - std::string area[] = {"晴", "雨夹雪", "雷阵雨伴有冰雹"}; - int num = rand() % (sizeof(area) / sizeof(area[0])); - return area[0]; - } - // 获取天气地区 - - std::string MainWeatherView::GetWeatherArea() - { - srand(time(0)); - std::string area[] = {"北京市东城区", "深圳市保安区"}; - int num = rand() % (sizeof(area) / sizeof(area[0])); - return area[1]; - } - // 获取最高气温和最低气温 - - std::string MainWeatherView::GetWeatherMaxAndMinTemperature() - { - // 使用系统时间作为种子 - srand(time(0)); - // 生成随机数 - int maxtemperature = 20 + rand() % 40; // 最高气温:20°~60° - int mintemperature = -5 + rand() % 20; - std::string MaxandMinTemperature = "最高" + std::to_string(22) + "°" + " " + "最低" + std::to_string(18) + "°"; - return MaxandMinTemperature; - } - - // 初始化标签 - UILabel *MainWeatherView::InitLabel(int x, int y, const char *text, const char *id, int textfont, int lineheight) - { - UILabel *label = new UILabel(); - label->SetPosition(x, y); - label->Resize(200, 30); - label->SetViewId(id); - label->SetStyle(STYLE_TEXT_FONT, textfont); - label->SetStyle(STYLE_LINE_HEIGHT, lineheight); - label->SetText(text); - label->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT); - label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); - label->SetRollSpeed(0); - label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, textfont); - label->SetVisible(true); - return label; - } - - void MainWeatherView::InitView(void) + void MainWeatherView::OnStart() { - if (container_ == nullptr) + View::OnStart(); + weatherView = new WeatherView(); + if (weatherView == nullptr) { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainWeatherView::new weatherView fail!"); return; } - // Add ScrollView to App - if (scrollView1_ == nullptr) - { - scrollView1_ = new UIScrollView(); - } - scrollView1_->SetPosition(0, 0); - scrollView1_->Resize(466, 466); - scrollView1_->SetViewId("scrollView1"); - scrollView1_->SetStyle(STYLE_BACKGROUND_OPA, 0xff); - scrollView1_->SetVerticalScrollState(false); // 设置纵向是否可滚动。 - scrollView1_->SetHorizontalScrollState(false); // 设置横向是否可滚动。 - container_->Add(scrollView1_); - scrollView1_->SetVisible(true); - /* -------------------------------------------------------------------------- */ - if (image1_ == nullptr) - { - image1_ = new UIImageView(); - } - image1_->SetPosition(109, 124); - image1_->Resize(100, 100); - image1_->SetViewId("image1"); - image1_->SetSrc(WeatherImag.c_str()); - scrollView1_->Add(image1_); - image1_->SetVisible(true); - /* -------------------------------------------------------------------------- */ - label1_ = InitLabel(185, 52, Time.c_str(), "label1", 40, 47); - scrollView1_->Add(label1_); - /* -------------------------------------------------------------------------- */ - label2_ = InitLabel(249, 126, Temperature.c_str(), "label2", 100, 125); - scrollView1_->Add(label2_); - /* -------------------------------------------------------------------------- */ - 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)); - scrollView1_->Add(label4_); - /* -------------------------------------------------------------------------- */ - label5_ = InitLabel(158, 368, MaxandMinTemperature.c_str(), "label5", 20, 23); - label5_->SetTextColor(Color::GetColorFromRGBA(0x99, 0x99, 0x99, 0xff)); - scrollView1_->Add(label5_); - /* -------------------------------------------------------------------------- */ - #if 0 - // Add ImageAnimatorView to App - if (imageAnimator1_ == nullptr) + UIView::OnDragListener *weatherDragListener = (UIView::OnDragListener *)OHOS::MainWeatherPresenter::GetInstance(); + if (weatherDragListener == nullptr) { - imageAnimator1_ = new UIImageAnimatorView(); - } - imageAnimator1_->SetPosition(213, 400); - imageAnimator1_->Resize(40, 40); - imageAnimator1_->SetViewId("imageAnimator1"); - imageAnimator1_->SetStyle(STYLE_BACKGROUND_OPA, 0xff); - - // Set additional property - imageAnimator1_->SetImageAnimatorSrc(g_imageAnimator1Info, 2, 200); - imageAnimator1_->SetTimeOfPause(200); - imageAnimator1_->SetRepeatTimes(1); - imageAnimator1_->SetReverse(true); - imageAnimator1_->Start(); - - // Add to scrollView1 - scrollView1_->Add(imageAnimator1_); - imageAnimator1_->SetVisible(true); - #endif - } - - void MainWeatherView::DeInitView() - { - container_->RemoveAll(); - scrollView1_->RemoveAll(); - if (label1_ != nullptr) - { - delete label1_; - label1_ = nullptr; - } - if (label2_ != nullptr) - { - delete label2_; - label2_ = nullptr; - } - if (image1_ != nullptr) - { - delete image1_; - image1_ = nullptr; - } - if (showWeatherDescriptionLabel != nullptr) - { - delete showWeatherDescriptionLabel; - showWeatherDescriptionLabel = nullptr; - } - if (label4_ != nullptr) - { - delete label4_; - label4_ = nullptr; - } - if (label5_ != nullptr) - { - delete label5_; - label5_ = nullptr; - } - if (imageAnimator1_ != nullptr) - { - imageAnimator1_->Stop(); // 视图退出后动画停止 - delete imageAnimator1_; - imageAnimator1_ = nullptr; - } - if (scrollView1_ != nullptr) - { - delete scrollView1_; - scrollView1_ = nullptr; - } - if (container_ != nullptr) - { - delete container_; - container_ = nullptr; - } - } - - void MainWeatherView::PreLoad(void) - { - if (!viewInitStatus) - { - InitView(); - viewInitStatus = true; - } - } - void MainWeatherView::OnStart() - { - View::OnStart(); - if (container_ == nullptr) - { - container_ = new UIViewGroup(); - container_->Resize(466, 466); - container_->SetTouchable(true); - container_->SetDraggable(true); - UIView::OnDragListener *draglistener = static_cast(MainWeatherPresenter::GetInstance()); - container_->SetOnDragListener(draglistener); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "weatherDragListener new fail"); + return; } - PreLoad(); - RootView::GetInstance()->Add(container_); + weatherView->SetOnDragListener(weatherDragListener); + weatherView->InitView(); + RootView::GetInstance()->Add(weatherView); // 增加按键监听 RootView::OnKeyActListener *keyactListener = (RootView::OnKeyActListener *)OHOS::MainWeatherPresenter::GetInstance(); RootView::GetInstance()->SetOnKeyActListener(keyactListener);