鸿蒙应用开发入门(三):开发第一个鸿蒙应用 原创

钟洪发老师
发布于 2020-12-21 17:36
浏览
2收藏

3.1 第一个鸿蒙应用实现需求
    编写两张页面,实现在第一张页面点击按钮跳转到第二张页面。在Java UI框架中,提供了两种编写布局的方式:在XML中声明UI布局和在代码中创建布局。这两种方式创建出的布局没有本质差别,都是我们需要熟悉方式,所以我们将通过XML的方式布局第一张页面,然后再通过代码的方式布局第二张页面。

 

3.2 用XML布局第一张页面

    1. 打开layout下面的“ability_main.xml”文件
    2. 在“ability_main.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:text="Hello World"
            ohos:text_color="white"
            ohos:text_size="32fp"
            ohos:center_in_parent="true"/>
    <Button
            ohos:id="$+id:button"
            ohos:width="match_content"
            ohos:height="match_content"
            ohos:text="Next"
            ohos:text_size="19fp"
            ohos:text_color="white"
            ohos:top_padding="8vp"
            ohos:bottom_padding="8vp"
            ohos:right_padding="80vp"
            ohos:left_padding="80vp"
            ohos:background_element="$graphic:background_button"
            ohos:below="$id:text"
            ohos:horizontal_center="true"
           />
</DependentLayout>

    3. 创建按钮的背景
    按钮的背景是通过“background_button”来指定的。右键点击“graphic”文件夹,选择“New > File”,命名为“background_button.xml”。

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

 

3.3 用编程的方式布局第二张页面
    1. 创建Feature Ability
    2. 代码编写界面

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(255, 255, 255));
        myLayout.setBackground(element);
        // 创建一个文本
        Text text = new Text(this);
        text.setText("Nice to meet you.");
        text.setTextSize(55);
        text.setTextColor(Color.BLACK);
        // 设置文本的布局
        DependentLayout.LayoutConfig textConfig = 
                                    new DependentLayout.LayoutConfig(MATCH_CONTENT,MATCH_CONTENT);
        textConfig.addRule(DependentLayout.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);
    }
}

 

3.4 实现页面跳转

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

 

    文章配套视频课程《鸿蒙手机应用开发入门》https://edu.51cto.com/course/26133.html

 

 

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
4
收藏 2
回复
举报
2条回复
按时间正序
/
按时间倒序
唐十
唐十

ohos:background_element="$graphic:background_button"

这一行的“$graphic:background_button”爆红是什么原因?

回复
2021-8-27 10:39:07
钟洪发老师
钟洪发老师 回复了 唐十
ohos:background_element="$graphic:background_button" 这一行的“$graphic:background_button”爆红是什么原因?

你把它换成颜色值就可以了比如:“#ccffff”,那就是一个背景,你没在graphic目录里建这个文件,就会爆红

回复
2021-8-27 14:11:00
回复
    相关推荐