欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

快速入門(mén)HarmonyOS的Java UI框架的教程

 更新時(shí)間:2020年09月18日 15:10:21   作者:編程教育的漏網(wǎng)之魚(yú)  
這篇文章主要介紹了快速入門(mén)HarmonyOS的Java UI框架,本文給大家介紹的非常詳細(xì)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

本文檔適用于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”。

0000000000011111111.20200917120417.34233295676813690694135023815559:50510917062253:2800:642CEF3864709AF4CA90D6E2D342923D671918358DBAB41F1CDF6076F5F4DABE.png?needInitFileName=true?needInitFileName=true

在“l(fā)ayout”文件夾下可以看到新增了“main_layout.xml”文件。

0000000000011111111.20200917120417.30532054029557177825168732061918:50510917062253:2800:4D84274B6A1E5680B97F231FCA1259AF3D0BDE26FEA1B0E693F096ABA1166CDA.png?needInitFileName=true?needInitFileName=true

打開(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”文件。

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>

說(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);
}
}

效果如圖所示:

0000000000011111111.20200917120417.17663833033072512715148579643111:50510917062253:2800:374BFE1A78208D3FFA61DC06089E102874789818F097FD8390DDA132FC96A594.png?needInitFileName=true?needInitFileName=true

創(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”文件。

0000000000011111111.20200909202823.74930964459566968760104266787246:50510909131850:2800:9C60847A938FFBF2A4F35040DAAEED568A32C98B7B8E5712967F988E3670991F.png?needInitFileName=true?needInitFileName=true?needInitFileName=true

代碼編寫(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論