HarmonyOS NEXT-开发基础知识总结

一、如何关闭UIability?

在点击事件中使用: this.context.terminateSelf();可以返回上一级界面
*注:切换后台管理,可以看到返回之前的界面,不知道是不是官方没有做杀死操作,或者有其他关闭方式。

二、设置沉浸式状态栏?

   // Main window is created, set main page for this ability
   hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');


   // 1.获取应用主窗口。
   let windowClass: window.Window | null = null;
   windowStage.getMainWindow((err: BusinessError, data) => {
     let errCode: number = err.code;
     if (errCode) {
       console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
       return;
     }
     windowClass = data;
     console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));

     // 2.实现沉浸式效果。方式一:设置导航栏、状态栏不显示。
      let names: Array<'status' | 'navigation'> = [];
      windowClass.setWindowSystemBarEnable(names, (err: BusinessError) => {
        let errCode: number = err.code;
        if (errCode) {
          console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err));
         return;
       }
       console.info('Succeeded in setting the system bar to be visible.');
      });
     // 2.实现沉浸式效果。方式二:设置窗口为全屏布局,配合设置导航栏、状态栏的透明度、背景/文字颜色及高亮图标等属性,与主窗口显示保持协调一致。
     let isLayoutFullScreen = true;
     windowClass.setWindowLayoutFullScreen(isLayoutFullScreen, (err: BusinessError) => {
       let errCode: number = err.code;
       if (errCode) {
         console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
         return;
       }
       console.info('Succeeded in setting the window layout to full-screen mode.');
     });
     let sysBarProps: window.SystemBarProperties = {
       statusBarColor: '#ff00ff',
       navigationBarColor: '#00ff00',
       // 以下两个属性从API Version 8开始支持
       statusBarContentColor: '#ffffff',
       navigationBarContentColor: '#ffffff'
     };
     windowClass.setWindowSystemBarProperties(sysBarProps, (err: BusinessError) => {
       let errCode: number = err.code;
       if (errCode) {
         console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err));
         return;
       }
       console.info('Succeeded in setting the system bar properties.');
     });
   });
   windowStage.loadContent('pages/idcard_scan_ability', (err, data) => {
     if (err.code) {
       hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
       return;
     }
     hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
   });
 }

三、Image组件实现图片的点击切换效果

android有selected、press等等状态,harmonyOS同样有一样的组件状态或者说是属性,也就是StateStyles,但是基于开发模式,image无法实现通过次状态达到图片切换的效果,如下图:


1703146022348.png

所以我在官方文档中找到了另一个方法:点击事件切换
这里的@State代表是动态状态,也就是变量改变后使用此变量的地方会自动更改,此方法目前使用无法达到切换效果,只能切换到第二个图片无法切换回第一张。


image.png
image.png

解决方案:
还是愚钝了,这个方式完美解决了上面的问题,置于为什么不能直接使用Resource方式直接动态变更这个就不太清楚了。

image.png

image.png

四、导入相册图片并保存到沙盒路径下

首先相册图片地址无法在c底层进行读取,所以一般需要转存到沙盒路径,或者使用fd传入c侧并在c侧使用fopen读取,不过这样需要重构下核心接口。
解决方案:

/**
   * 转存文件
* destName : /test.jpg
   */
  public static async savePicFile(srcPath: string, destName: string): Promise<string> {
    try {
      let filesDir = context.filesDir;
      // 删除缓存(必须操作否则拷贝失败)
      if (fs.access(filesDir + destName)) {
        fs.unlink(filesDir + destName)
      }
      // 打开文件
      let srcFile = fs.openSync(srcPath, fs.OpenMode.READ_ONLY);
     // 拷贝文件,必须使用此方法 其他读写方式会无法成功读取文件
      fs.copyFileSync(srcFile.fd, filesDir + destName)
      fs.closeSync(srcFile);
      return filesDir + destName;
    } catch (error) {
      let err = error as BusinessError;
      console.error(IdcardConfig.SDK_TAG + 'Failed to savePicFile. errorCode = ' + err.code);
      return "";
    }
  }

五、保留两位小数,并四舍五入

value = Math.round(value * 100) / 100;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容