> 文档中心 > HarmonyOS-HelloWorld

HarmonyOS-HelloWorld


概述

HarmonyOS自从面世以来,零零碎碎接触了不少,本文算是开始系统的第一步。

编码

安装好HUAWEI DevEco Studio,配置好环境,在Device为Phone的情况下创建新的Empty Feature Ability(Java),一路点下去即可创建空白项目。
查看项目发现有些文件已经被自动创建,比如entry包就和Android的app包差不多,MyApplication就和Android的Application相似,MainAbility和MainActivity也差多。

1.编写xml布局

ability_main.xml已经被自动创建,本文就在ability_main上修改的。

<DependentLayout    xmlns:ohos="http://schemas.huawei.com/res/ohos"    ohos:height="match_parent"    ohos:width="match_parent"    ohos:background_element="#ffffff">    <Text ohos:id="$+id:text" ohos:height="match_content" ohos:width="match_content" ohos:center_in_parent="true" ohos:text="xml布局" ohos:text_color="#000000" ohos:text_font="monospace" ohos:text_size="20fp"/>    <Button ohos:id="$+id:button" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_button" ohos:below="$id:text" ohos:bottom_padding="8vp" ohos:horizontal_center="true" ohos:left_padding="32vp" ohos:right_padding="32vp" ohos:text="下一步" ohos:text_color="#ffffff" ohos:text_size="20fp" ohos:top_margin="32vp" ohos:top_padding="8vp"/></DependentLayout>

DependentLayout和Android中RelativeLayout相似,都属于相对布局,里面包裹的子元素如果比较多的话,一般用id定位,各种属性使用也都差不多。background_element引用的background_button是在单独绘制的圆角矩形,否则简单设置颜色就是一个四角为90°的矩形。文字的大小单位一般使用fp,如果不加单位默认为px,使用px在不同的设备上可能会导致布局有较大偏差,而fp中处理了屏幕像素密度,会有比较好的适配效果。距离单位一般使用vp,道理和fp一样,这一点和Android中的sp和dp一样。

2.为button创建点击事件
public class MainAbilitySlice extends AbilitySlice {    @Override    public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); Button button = (Button) findComponentById(ResourceTable.Id_button); button.setClickedListener(component -> {      Intent intent0 = new Intent();      Operation operation = (Operation) new Intent.OperationBuilder().withDeviceId("").withBundleName("com.harmonyos.helloworld").withAbilityName(".SecondAbility").build();      intent1.setOperation(operation);      startAbility(intent1);  } );    }    ...

通过Id_button找到xml中button,然后设置点击事件。withDeviceId中设置为“”,是因为当设置“”时就会自动获取该设备id;withBundleName一般里面填写config.json中的bundleName,bundleName一般和包名一致;withAbilityName中需要填写跳转目标的name,这个config.json也有配置,若目标name和bundleName有包名重合,包名可以不写,就像上述代码".SecondAbility"一样。接下来就是创建SecondAbility布局。

3. 编码布局

SecondAbility布局这里采用编码布局。xml布局和编码布局本质上是一样的,只是形式上不一样。在创建好的SecondAbility中的SecondAbilitySlice中使用编码方式绘制页面

    @Override    public void onStart(Intent intent) { super.onStart(intent); // 创建父容器 DependentLayout dependentLayout = new DependentLayout(this); // 指定父容器宽高 dependentLayout.setWidth(MATCH_PARENT); dependentLayout.setHeight(MATCH_PARENT); // 设置父容器的背景颜色 ShapeElement shapeElement = new ShapeElement(); shapeElement.setRgbColor(new RgbColor(244, 255, 255)); dependentLayout.setBackground(shapeElement); // 创建文本容器 Text text = new Text(this); // 指定容器的宽高为包裹内容 text.setWidth(MATCH_CONTENT); text.setHeight(MATCH_CONTENT); // 设置字体颜色 text.setTextColor(Color.BLACK); // 设置内容 text.setText("编码布局"); // 设置字体大小 text.setTextSize(20, Text.TextSizeType.FP); // 设置文本容器在父容器中居中对齐 DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig(MATCH_CONTENT, MATCH_CONTENT); layoutConfig.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT); text.setLayoutConfig(layoutConfig); // 将文本添加进父容器中 dependentLayout.addComponent(text); // 将父容器设置为该页面的页面布局 super.setUIContent(dependentLayout);    }

1.创建父容器DependentLayout ,同时指定父容器的大小、形状和背景。
2.创建文本,设置文本文案,指定字体颜色和大小。
3.配置文本的布局属性,包裹内容(内容有多大,容器有多大)和居中父容器。
4.将创建好的文本添加进父容器中。
5.将父容器设置为该页面的布局容器。

4.编译运行

在这里插入图片描述
主页面中展示xml布局和下一步按钮,点击下一步进入编码布局。

总结

  • 本文主要是熟悉HarmonyOS项目的一些基本配置,简单写个小小的demo,算是系统第一步。
  • 熟悉Android的开发者可以在其中一一找出与Android相对应的控件、用法、文件等,所以对于Android开发者来讲,HarmonyOS应用开发可以轻松上手。虽然两者编码相似,甚至有些连用户体验可能相似,但HarmonyOS绝对不是换壳的Android,HarmonyOS在统筹规划、分布式、万物互联、安全等方面肯定是走在前面的,两者各有优劣,选择适合的就好。