用DefaultDimension获得某成本中心的值

// 用DefaultDimension获得某一个成本中心的值。
// 用法:getDimensionValueTID(common.defaultDimension, "Center");
public static str 255 getDimensionValueTID(RecId defaultDimension, Name dimName)
{
    DimensionAttributeValueSetStorage dimStorage;
    ;
 
    dimStorage = DimensionAttributeValueSetStorage::find(defaultDimension);
 
    return dimStorage.getDisplayValueByDimensionAttribute(DimensionAttribute::findByName(dimName).RecId);
}
<think>我们被要求回答关于鸿蒙开发的问题。鸿蒙系统(HarmonyOS)是华为推出的分布式操作系统。开发鸿蒙应用主要使用两种语言:Java和JS(用于快速开发)。我们将重点介绍使用JS的FA(FeatureAbility)开发,因为这是目前较常用的方式。问题:你会鸿蒙开发吗?回答:是的,我熟悉鸿蒙应用开发。以下我将通过一个简单的“Hello World”示例来展示鸿蒙应用的基本结构,并解释关键代码。我们将创建一个简单的鸿蒙应用,它包含一个文本显示和一个按钮,点击按钮会改变文本内容。步骤:1.创建项目(使用DevEco Studio)2.编写布局文件(XML)3.编写逻辑文件(JS)具体代码:首先,在项目的`resources`目录下,我们有一个布局文件`ability_main.xml`(位于`resources/base/layout/`): ```xml<?xmlversion="1.0"encoding="utf-8"?> <DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:width="match_parent"ohos:height="match_parent"ohos:alignment="center"ohos:orientation="vertical"><Textohos:id="$+id:text_hello"ohos:width="match_content"ohos:height="match_content"ohos:text="HelloWorld"ohos:text_size="32fp"/><Buttonohos:id="$+id:btn_change"ohos:width="match_content"ohos:height="match_content"ohos:text="Change Text"ohos:top_margin="20vp"/></DirectionalLayout> ```然后,在`js`目录下的`default`目录中,我们有一个`pages`目录,里面是各个页面的逻辑。我们在`main`页面(即`pages/index/index.js`)中编写逻辑: ```javascript// index.jsexportdefault {data: {text: 'Hello World'},onInit() {//页面初始化},changeText(){//改变文本内容this.text ='HelloHarmonyOS!';//更新到UIthis.$element('text_hello').setText(this.text);}} ```但是,上述代码中,我们还没有将按钮的点击事件绑定到`changeText`方法。因此,我们需要在`index.js`的同级目录下,有一个`index.hml`文件(用于描述页面结构):```html<!--index.hml --><div class="container"><textclass="title" id="text_hello">{{ text}}</text><buttonclass="btn"value="ChangeText"onclick="changeText"></button> </div>```注意:在鸿蒙的JS开发中,我们使用`.hml`、`.js`和`.css`文件来分别描述页面结构、逻辑和样式。然而,上面的示例中,我们使用了两种不同的布局方式(XML和HML)。实际上,鸿蒙的JS应用开发使用HML+JS+CSS,而Java应用开发使用XML布局。为了统一,我们按照JS FA的开发方式,使用HML。因此,我们调整一下,使用HML来写页面,并搭配JS和CSS。完整的示例:1.`index.hml`:```html<divclass="container"><text class="title">{{ text}}</text><inputtype="button"class="btn"value="Change Text" @click="changeText"/> </div>```2.`index.js`:```javascriptexportdefault {data: {text: 'Hello World'},changeText() {this.text ='HelloHarmonyOS!';}} ```3. `index.css`:```css.container{flex-direction: column;justify-content: center;align-items: center;width:100%;height:100%; }.title {font-size:38px;color:#000000;margin-bottom:20px;} .btn{width:200px;height:50px;background-color:#007dff;font-size:24px;text-color:white;} ```解释: -HML文件:使用类HTML的标签,如`div`、`text`、`input`(这里按钮用`input[type=button]`或`button`标签)。通过`{{}}`绑定数据,通过`@click`绑定事件。 -JS文件:定义页面的数据和方法。在`data`中定义的数据可以在HML中绑定。方法直接在对象中定义,如`changeText`。- CSS文件:定义样式,与Web开发类似。这个示例应用运行后,会显示一个文本和一个按钮,点击按钮文本会从“Hello World”变成“HelloHarmonyOS!”。鸿蒙开发还涉及很多其他方面,如分布式能力、多设备适配、服务卡片等。§§相关问题§§:1.鸿蒙应用开发中Java和JS两种开发方式的主要区别是什么?2.如何实现鸿蒙应用在不同设备上的自适应布局?3.鸿蒙的分布式能力在代码层面如何实现?4.如何开发一个鸿蒙的服务卡片(Service Widget)?</think># 鸿蒙开发详解 是的,我精通鸿蒙(HarmonyOS)开发。鸿蒙是华为推出的分布式操作系统,支持多种设备类型(手机、平板、手表、电视等)。下面我将详细介绍鸿蒙开发的关键方面,并提供一个完整的示例应用。 ## 鸿蒙开发基础 ### 开发环境配置 1. 下载并安装 [DevEco Studio](https://developer.harmonyos.com/cn/develop/deveco-studio) 2. 配置Java SDK(建议JDK 11) 3. 创建项目时选择设备类型和模板 ### 核心概念 - **Ability**:应用的基本组成部分,分为Page Ability(UI)和Service Ability(后台服务) - **FA/PA**:Feature Ability(功能)和Particle Ability(粒子能力) - **HAP**:Harmony Ability Package,应用分发格式 - **分布式能力**:跨设备协同工作的核心特性 ## 示例应用:天气查询应用 下面是一个完整的鸿蒙应用示例,展示天气查询功能: ### 项目结构 ``` src/ ├── main/ │ ├── config.json # 应用配置文件 │ ├── resources/ # 资源文件 │ ├── java/ # Java代码 │ └── js/ # JavaScript代码 ``` ### 1. 配置文件 (config.json) ```json { "app": { "bundleName": "com.example.weather", "vendor": "example", "version": { "code": 1, "name": "1.0" } }, "deviceConfig": {}, "module": { "package": "com.example.weather", "name": ".MyApplication", "mainAbility": "com.example.weather.MainAbility", "deviceType": ["phone", "tablet"], "abilities": [ { "name": "MainAbility", "icon": "$media:icon", "label": "Weather App", "launchType": "standard", "type": "page", "visible": true } ] } } ``` ### 2. 布局文件 (weather_layout.xml) ```xml <?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:width="match_parent" ohos:height="match_parent" ohos:orientation="vertical" ohos:padding="32"> <Text ohos:id="$+id:locationText" ohos:width="match_parent" ohos:height="50vp" ohos:text="当前位置: 北京" ohos:text_size="25fp" ohos:text_alignment="center"/> <Image ohos:id="$+id:weatherIcon" ohos:width="150vp" ohos:height="150vp" ohos:image_src="$media:sunny" ohos:alignment="center"/> <Text ohos:id="$+id:temperatureText" ohos:width="match_parent" ohos:height="80vp" ohos:text="25°C" ohos:text_size="60fp" ohos:text_alignment="center"/> <Text ohos:id="$+id:descriptionText" ohos:width="match_parent" ohos:height="40vp" ohos:text="晴朗" ohos:text_size="30fp" ohos:text_alignment="center"/> <Button ohos:id="$+id:refreshButton" ohos:width="match_parent" ohos:height="50vp" ohos:text="刷新天气" ohos:top_margin="20vp"/> </DirectionalLayout> ``` ### 3. Java代码 (MainAbilitySlice.java) ```java package com.example.weather.slice; import com.example.weather.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Button; import ohos.agp.components.Component; import ohos.agp.components.Image; import ohos.agp.components.Text; import ohos.app.dispatcher.TaskDispatcher; import ohos.app.dispatcher.task.TaskPriority; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; public class MainAbilitySlice extends AbilitySlice { // 日志标签 private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00201, "WeatherApp"); // 天气数据模型 private static class WeatherData { String location; String temperature; String description; int iconResource; } @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_weather_layout); // 初始化UI组件 Text locationText = (Text) findComponentById(ResourceTable.Id_locationText); Image weatherIcon = (Image) findComponentById(ResourceTable.Id_weatherIcon); Text temperatureText = (Text) findComponentById(ResourceTable.Id_temperatureText); Text descriptionText = (Text) findComponentById(ResourceTable.Id_descriptionText); Button refreshButton = (Button) findComponentById(ResourceTable.Id_refreshButton); // 刷新按钮点击事件 refreshButton.setClickedListener(component -> { HiLog.info(LABEL_LOG, "刷新天气按钮被点击"); fetchWeatherData(locationText, weatherIcon, temperatureText, descriptionText); }); // 初始加载天气数据 fetchWeatherData(locationText, weatherIcon, temperatureText, descriptionText); } // 获取天气数据(模拟API调用) private void fetchWeatherData(Text locationText, Image weatherIcon, Text temperatureText, Text descriptionText) { // 使用异步任务防止阻塞UI TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT); globalTaskDispatcher.asyncDispatch(() -> { // 模拟网络请求延迟 try { Thread.sleep(1500); } catch (InterruptedException e) { HiLog.error(LABEL_LOG, "网络请求中断: " + e.getMessage()); } // 创建模拟天气数据 WeatherData weather = new WeatherData(); weather.location = "北京"; weather.temperature = "25°C"; weather.description = "晴朗"; weather.iconResource = ResourceTable.Media_sunny; // 更新UI getUITaskDispatcher().asyncDispatch(() -> { locationText.setText("当前位置: " + weather.location); weatherIcon.setPixelMap(weather.iconResource); temperatureText.setText(weather.temperature); descriptionText.setText(weather.description); }); }); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } } ``` ### 4. 资源文件 (resources/base/media) - 添加天气图标:sunny.png, cloudy.png, rainy.png ### 5. 权限配置 (config.json 添加) ```json "reqPermissions": [ { "name": "ohos.permission.INTERNET" }, { "name": "ohos.permission.LOCATION" } ] ``` ## 鸿蒙关键特性解释 ### 1. 分布式能力 鸿蒙的核心优势是分布式能力,允许设备间无缝协作: ```java // 设备发现 DeviceManager deviceManager = DeviceManager.getInstance(); List<DeviceInfo> devices = deviceManager.getTrustedDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE); // 跨设备调用 Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder() .withDeviceId(remoteDeviceId) .withBundleName("com.example.remoteapp") .withAbilityName("com.example.remoteapp.RemoteAbility") .build(); intent.setOperation(operation); startAbility(intent); ``` ### 2. 原子化服务 鸿蒙支持原子化服务(免安装应用): ```json // config.json 配置 "abilities": [ { "name": "WeatherServiceAbility", "type": "service", "visible": true, "formsEnabled": true, // 启用服务卡片 "forms": [ { "name": "weather_widget", "description": "天气卡片", "type": "JS", "jsComponentName": "widget", "colorMode": "auto", "isDefault": true, "updateEnabled": true, "scheduledUpdateTime": "10:30", "updateDuration": 1, "supportDimensions": ["2*2"], "defaultDimension": "2*2" } ] } ] ``` ### 3. UI开发框架 鸿蒙提供两种UI开发方式: - **Java UI框架**:面向Java开发者 - **JS UI框架**:面向Web开发者,类似小程序开发 JS UI示例 (weather.hml): ```html <div class="container"> <text class="location">{{ location }}</text> <image class="icon" src="{{ iconPath }}"></image> <text class="temperature">{{ temperature }}</text> <text class="description">{{ description }}</text> <button class="refresh-btn" onclick="refreshWeather">刷新</button> </div> ``` ## 鸿蒙开发最佳实践 1. **多设备适配**: ```java // 检测设备类型 DeviceInfo deviceInfo = DeviceInfoManager.getDeviceInfo(); if (deviceInfo.getDeviceType() == DeviceInfo.DEVICE_TYPE_WATCH) { // 手表特定逻辑 } else if (deviceInfo.getDeviceType() == DeviceInfo.DEVICE_TYPE_TV) { // 电视特定逻辑 } ``` 2. **性能优化**: - 使用异步任务处理耗时操作 - 合理使用内存缓存 - 优化布局层次结构 3. **安全实践**: ```java // 权限请求 if (verifySelfPermission("ohos.permission.LOCATION") != 0) { requestPermissionsFromUser(new String[]{"ohos.permission.LOCATION"}, 0); } ``` 4. **测试与调试**: - 使用DevEco Studio的模拟器 - HiLog日志系统 - 分布式调试工具 ##
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值