当前位置: 移动技术网 > IT编程>开发语言>Java > 快速入门HarmonyOS的Java UI框架的教程

快速入门HarmonyOS的Java UI框架的教程

2020年09月19日  | 移动技术网IT编程  | 我要评论
本文档适用于harmonyos应用开发的初学者。编写两个简单的页面,实现在第一个页面点击按钮跳转到第二个页面。注意:运行hello world在创建工程时,设备类型和模板分别以wearable和emp

本文档适用于harmonyos应用开发的初学者。编写两个简单的页面,实现在第一个页面点击按钮跳转到第二个页面。

注意:运行hello world在创建工程时,设备类型和模板分别以wearable和empty feature ability(java)为例,本文档也基于相同的设备类型和模板进行说明。

编写第一个页面

在java ui框架中,提供了两种编写布局的方式:在xml中声明ui布局和在代码中创建布局。这两种方式创建出的布局没有本质差别,为了熟悉两种方式,我们将通过xml的方式编写第一个页面,通过代码的方式编写第二个页面。

xml编写页面

在“project”窗口,打开“entry > src > main > resources > base”,右键点击“base”文件夹,选择“new > directory”,命名为“layout”。

键点击“layout”文件夹,选择“new > file”,命名为“main_layout.xml”。

0000000000011111111.20200917120417.34233295676813690694135023815559:50510917062253:2800:642cef3864709af4ca90d6e2d342923d671918358dbab41f1cdf6076f5f4dabe.png?needinitfilename=true?needinitfilename=true

在“layout”文件夹下可以看到新增了“main_layout.xml”文件。

0000000000011111111.20200917120417.30532054029557177825168732061918:50510917062253:2800:4d84274b6a1e5680b97f231fca1259af3d0bde26fea1b0e693f096aba1166cda.png?needinitfilename=true?needinitfilename=true

打开“main_layout.xml”文件,添加一个文本和一个按钮,示例代码如下:

<?xml version="1.0" encoding="utf-8"?>
<dependentlayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:width="match_parent"
    ohos:height="match_parent"
    ohos:background_element="#000000">
  <text
      ohos:id="$+id:text"
      ohos:width="match_content"
      ohos:height="match_content"
      ohos:center_in_parent="true"
      ohos:text="hello world"
      ohos:text_color="white"
      ohos:text_size="32fp"/>
  <button
      ohos:id="$+id:button"
      ohos:width="match_content"
      ohos:height="match_content"
      ohos:text_size="19fp"
      ohos:text="next"
      ohos:top_padding="8vp"
      ohos:bottom_padding="8vp"
      ohos:right_padding="80vp"
      ohos:left_padding="80vp"
      ohos:text_color="white"
      ohos:background_element="$graphic:button_element"
      ohos:center_in_parent="true"
      ohos:align_parent_bottom="true"/>
</dependentlayout>

上述按钮的背景是通过“button_element”来显示的,需要在“base”目录下创建“graphic”文件夹,在“graphic”文件夹中新建一个“button_element.xml”文件。

0000000000011111111.20200917120417.20657229793966139327199575643230:50510917062253:2800:995a3f1c2739d797d7a35b1e965d068c864946baafa47541a7690cc377796c91.png?needinitfilename=true?needinitfilename=true

“button_element.xml”的示例代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:shape="oval">
  <solid
      ohos:color="#007dff"/>
</shape>

说明:如果deveco studio提示xmlns字段错误,请忽略,不影响后续操作。

加载xml布局

在“project”窗口中,选择“entry > src > main > java > com.example.helloworld > slice” ,打开“mainabilityslice.java”文件。重写onstart()方法加载xml布局,示例代码如下:

package com.example.myapplication.slice;
 
import com.example.myapplication.resourcetable;
import ohos.aafwk.ability.abilityslice;
import ohos.aafwk.content.intent;
 
public class mainabilityslice extends abilityslice {
 
@override
public void onstart(intent intent) {
super.onstart(intent);
super.setuicontent(resourcetable.layout_main_layout); // 加载xml布局
}
 
@override
public void onactive() {
super.onactive();
}
 
@override
public void onforeground(intent intent) {
super.onforeground(intent);
}
}

效果如图所示:

0000000000011111111.20200917120417.17663833033072512715148579643111:50510917062253:2800:374bfe1a78208d3ffa61dc06089e102874789818f097fd8390dda132fc96a594.png?needinitfilename=true?needinitfilename=true

创建另一个页面

创建feature ability

在“project”窗口,打开“entry > src > main > java”,右键点击“com.example.myapplication”文件夹,选择“new > ability > empty feature ability(java)”。配置ability时,将“page name”设置为“secondability”,点击“finish”。创建完成后,可以看到新增了“secondability”和“secondabilityslice”文件。

0000000000011111111.20200909202823.74930964459566968760104266787246:50510909131850:2800:9c60847a938ffbf2a4f35040daaeed568a32c98b7b8e5712967f988e3670991f.png?needinitfilename=true?needinitfilename=true?needinitfilename=true

代码编写界面

在上一节中,我们用xml的方式编写了一个包含文本和按钮的页面。为了帮助开发者熟悉在代码中创建布局的方式,接下来我们使用此方式编写第二个页面。

打开 “secondabilityslice.java”文件,添加一个文本,示例代码如下:

package com.example.myapplication.slice;
 
import ohos.aafwk.ability.abilityslice;
import ohos.aafwk.content.intent;
import ohos.agp.colors.rgbcolor;
import ohos.agp.components.dependentlayout;
import ohos.agp.components.dependentlayout.layoutconfig;
import ohos.agp.components.text;
import ohos.agp.components.element.shapeelement;
import ohos.agp.utils.color;
 
import static ohos.agp.components.componentcontainer.layoutconfig.match_parent;
import static ohos.agp.components.componentcontainer.layoutconfig.match_content;
 
public class secondabilityslice extends abilityslice {
 
  @override
  public void onstart(intent intent) {
    super.onstart(intent);
    // 声明布局
    dependentlayout mylayout = new dependentlayout(this);
    // 设置布局大小
    mylayout.setwidth(match_parent);
    mylayout.setheight(match_parent);
    shapeelement element = new shapeelement();
    element.setrgbcolor(new rgbcolor(0, 0, 0));
    mylayout.setbackground(element);
 
    // 创建一个文本
    text text = new text(this);
    text.settext("nice to meet you.");
    text.setwidth(match_parent);
    text.settextsize(55);
    text.settextcolor(color.white);
    // 设置文本的布局
    dependentlayout.layoutconfig textconfig = new dependentlayout.layoutconfig(match_content,match_content);
    textconfig.addrule(layoutconfig.center_in_parent);
    text.setlayoutconfig(textconfig);
    mylayout.addcomponent(text);
 
    super.setuicontent(mylayout);
  }
 
  @override
  public void onactive() {
    super.onactive();
  }
 
  @override
  public void onforeground(intent intent) {
    super.onforeground(intent);
  }
}

实现页面跳转

打开第一个页面的“mainabilityslice.java”文件,重写onstart()方法添加按钮的响应逻辑,实现点击按钮跳转到下一页,示例代码如下:

package com.example.myapplication.slice;
 
import com.example.myapplication.resourcetable;
import ohos.aafwk.ability.abilityslice;
import ohos.aafwk.content.intent;
import ohos.aafwk.content.operation;
import ohos.agp.components.*;
 
public class mainabilityslice extends abilityslice {
 
  @override
  public void onstart(intent intent) {
    super.onstart(intent);
    super.setuicontent(resourcetable.layout_main_layout);
    button button = (button) findcomponentbyid(resourcetable.id_button);
 
    if (button != null) {
      // 为按钮设置点击回调
      button.setclickedlistener(new component.clickedlistener() {
        @override
        public void onclick(component component) {
        intent secondintent = new intent();
        // 指定待启动fa的bundlename和abilityname
        operation operation = new intent.operationbuilder()
            .withdeviceid("")
            .withbundlename("com.example.myapplication")
            .withabilityname("com.example.myapplication.secondability")
            .build();
        secondintent.setoperation(operation);
        startability(secondintent); // 通过abilityslice的startability接口实现启动另一个页面
        }
      });
    }
  }
 
  @override
  public void onactive() {
    super.onactive();
  }
 
  @override
  public void onforeground(intent intent) {
    super.onforeground(intent);
  }
}

再次运行项目,效果如图所示:

总结

到此这篇关于快速入门harmonyos的java ui框架的文章就介绍到这了,更多相关harmonyos的java ui框架内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网