快速入門(mén)HarmonyOS的Java UI框架的教程
本文檔適用于HarmonyOS應(yīng)用開(kāi)發(fā)的初學(xué)者。編寫(xiě)兩個(gè)簡(jiǎn)單的頁(yè)面,實(shí)現(xiàn)在第一個(gè)頁(yè)面點(diǎn)擊按鈕跳轉(zhuǎn)到第二個(gè)頁(yè)面。
注意:運(yùn)行Hello World在創(chuàng)建工程時(shí),設(shè)備類(lèi)型和模板分別以Wearable和Empty Feature Ability(Java)為例,本文檔也基于相同的設(shè)備類(lèi)型和模板進(jìn)行說(shuō)明。
編寫(xiě)第一個(gè)頁(yè)面
在Java UI框架中,提供了兩種編寫(xiě)布局的方式:在XML中聲明UI布局和在代碼中創(chuàng)建布局。這兩種方式創(chuàng)建出的布局沒(méi)有本質(zhì)差別,為了熟悉兩種方式,我們將通過(guò)XML的方式編寫(xiě)第一個(gè)頁(yè)面,通過(guò)代碼的方式編寫(xiě)第二個(gè)頁(yè)面。
XML編寫(xiě)頁(yè)面
在“Project”窗口,打開(kāi)“entry > src > main > resources > base”,右鍵點(diǎn)擊“base”文件夾,選擇“New > Directory”,命名為“l(fā)ayout”。
鍵點(diǎn)擊“l(fā)ayout”文件夾,選擇“New > File”,命名為“main_layout.xml”。
在“l(fā)ayout”文件夾下可以看到新增了“main_layout.xml”文件。
打開(kāi)“main_layout.xml”文件,添加一個(gè)文本和一個(gè)按鈕,示例代碼如下:
<?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>
上述按鈕的背景是通過(guò)“button_element”來(lái)顯示的,需要在“base”目錄下創(chuàng)建“graphic”文件夾,在“graphic”文件夾中新建一個(gè)“button_element.xml”文件。
“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>
說(shuō)明:如果DevEco Studio提示xmlns字段錯(cuò)誤,請(qǐng)忽略,不影響后續(xù)操作。
加載XML布局
在“Project”窗口中,選擇“entry > src > main > java > com.example.helloworld > slice” ,打開(kāi)“MainAbilitySlice.java”文件。重寫(xiě)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); } }
效果如圖所示:
創(chuàng)建另一個(gè)頁(yè)面
創(chuàng)建Feature Ability
在“Project”窗口,打開(kāi)“entry > src > main > java”,右鍵點(diǎn)擊“com.example.myapplication”文件夾,選擇“New > Ability > Empty Feature Ability(Java)”。配置Ability時(shí),將“Page Name”設(shè)置為“SecondAbility”,點(diǎn)擊“Finish”。創(chuàng)建完成后,可以看到新增了“SecondAbility”和“SecondAbilitySlice”文件。
代碼編寫(xiě)界面
在上一節(jié)中,我們用XML的方式編寫(xiě)了一個(gè)包含文本和按鈕的頁(yè)面。為了幫助開(kāi)發(fā)者熟悉在代碼中創(chuàng)建布局的方式,接下來(lái)我們使用此方式編寫(xiě)第二個(gè)頁(yè)面。
打開(kāi) “SecondAbilitySlice.java”文件,添加一個(gè)文本,示例代碼如下:
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); // 設(shè)置布局大小 myLayout.setWidth(MATCH_PARENT); myLayout.setHeight(MATCH_PARENT); ShapeElement element = new ShapeElement(); element.setRgbColor(new RgbColor(0, 0, 0)); myLayout.setBackground(element); // 創(chuàng)建一個(gè)文本 Text text = new Text(this); text.setText("Nice to meet you."); text.setWidth(MATCH_PARENT); text.setTextSize(55); text.setTextColor(Color.WHITE); // 設(shè)置文本的布局 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); } }
實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
打開(kāi)第一個(gè)頁(yè)面的“MainAbilitySlice.java”文件,重寫(xiě)onStart()方法添加按鈕的響應(yīng)邏輯,實(shí)現(xiàn)點(diǎn)擊按鈕跳轉(zhuǎn)到下一頁(yè),示例代碼如下:
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) { // 為按鈕設(shè)置點(diǎn)擊回調(diào) button.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { Intent secondIntent = new Intent(); // 指定待啟動(dòng)FA的bundleName和abilityName Operation operation = new Intent.OperationBuilder() .withDeviceId("") .withBundleName("com.example.myapplication") .withAbilityName("com.example.myapplication.SecondAbility") .build(); secondIntent.setOperation(operation); startAbility(secondIntent); // 通過(guò)AbilitySlice的startAbility接口實(shí)現(xiàn)啟動(dòng)另一個(gè)頁(yè)面 } }); } } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }
再次運(yùn)行項(xiàng)目,效果如圖所示:
總結(jié)
到此這篇關(guān)于快速入門(mén)HarmonyOS的Java UI框架的文章就介紹到這了,更多相關(guān)HarmonyOS的Java UI框架內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 鴻蒙HarmonyOS開(kāi)發(fā):Navigation路由導(dǎo)航功能和實(shí)踐
- 鴻蒙開(kāi)發(fā)之Button按鈕類(lèi)型及如何通過(guò)代碼設(shè)置(HarmonyOS鴻蒙開(kāi)發(fā)基礎(chǔ)知識(shí))
- HarmonyOS鴻蒙基本控件的實(shí)現(xiàn)
- 鴻蒙(HarmonyOS)實(shí)現(xiàn)隱私政策彈窗效果
- 鴻蒙開(kāi)發(fā)之處理圖片位圖操作的方法詳解(HarmonyOS鴻蒙開(kāi)發(fā)基礎(chǔ)知識(shí))
- HarmonyOS系統(tǒng)利用AVPlayer開(kāi)發(fā)視頻播放功能
相關(guān)文章
SpringBoot使用Thymeleaf自定義標(biāo)簽的實(shí)例代碼
這篇文章主要介紹了SpringBoot使用Thymeleaf自定義標(biāo)簽的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09java二叉查找樹(shù)的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了java二叉查找樹(shù)的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08SpringBoot擴(kuò)展SpringMVC原理并實(shí)現(xiàn)全面接管
這篇文章主要介紹了SpringBoot擴(kuò)展SpringMVC原理并實(shí)現(xiàn)全面接管,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11SpringBoot監(jiān)聽(tīng)Nacos動(dòng)態(tài)修改日志級(jí)別的操作方法
線(xiàn)上系統(tǒng)的日志級(jí)別一般都是 INFO 級(jí)別,有時(shí)候需要查看 WARN 級(jí)別的日志,所以需要?jiǎng)討B(tài)修改日志級(jí)別,微服務(wù)項(xiàng)目中使用 Nacos 作為注冊(cè)中心,我們可以監(jiān)聽(tīng) Nacos 配置,修改日志級(jí)別,這篇文章主要介紹了SpringBoot監(jiān)聽(tīng)Nacos動(dòng)態(tài)修改日志級(jí)別的操作方法,需要的朋友可以參考下2023-12-12java實(shí)現(xiàn)獲取網(wǎng)站的keywords,description
這篇文章主要介紹了java實(shí)現(xiàn)獲取網(wǎng)站的keywords,description的相關(guān)資料,需要的朋友可以參考下2015-03-03Java實(shí)現(xiàn)HashMap排序方法的示例詳解
這篇文章主要通過(guò)一些示例為大家介紹了Java對(duì)HashMap進(jìn)行排序的方法,幫助大家更好的理解和使用Java,感興趣的朋友可以了解一下2022-05-05將java程序打包成可執(zhí)行文件的實(shí)現(xiàn)方式
本文介紹了將Java程序打包成可執(zhí)行文件的三種方法:手動(dòng)打包(將編譯后的代碼及JRE運(yùn)行環(huán)境一起打包),使用第三方打包工具(如Launch4j)和JDK自帶工具(jpackage),每種方法都有其優(yōu)缺點(diǎn),可根據(jù)實(shí)際需求選擇合適的方式2025-02-02簡(jiǎn)單說(shuō)說(shuō)Java SE、Java EE、Java ME三者之間的區(qū)別
本篇文章小編就為大家簡(jiǎn)單說(shuō)說(shuō)Java SE、Java EE、Java ME三者之間的區(qū)別。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10java中讀寫(xiě)Properties屬性文件公用方法詳解
在項(xiàng)目開(kāi)發(fā)中我們會(huì)將很多環(huán)境特定的變量定義到一個(gè)配置文件中,比如properties文件,把數(shù)據(jù)庫(kù)的用戶(hù)名和密碼存放到此屬性文件中。下面這篇文章就主要介紹了java中讀寫(xiě)Properties屬性文件公用方法,需要的朋友可以參考借鑒。2017-01-01