Harmony OS 开发入门HelloWorld

HUAWEI_Engineer
发布于 2021-10-24 11:59
浏览
3收藏

1.MainAbilitySlice
package com.example.app_java.slice;

import com.example.app_java.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//拿到按钮
Button btn =(Button)findComponentById(ResourceTable.Id_btn);
//注册按钮事件
btn.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
//事件处理程序
Intent intent1 = new Intent();
intent1.setParam(“name”,“Hello Harmony OS”);
present(new SecondAbilitySlice(),intent1);
}
});

}

@Override
public void onActive() {
    super.onActive();
}

@Override
public void onForeground(Intent intent) {
    super.onForeground(intent);
}

}
2.SecondAbilitySlice
package com.example.app_java.slice;

import com.example.app_java.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;

public class SecondAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_second);

    //接收MainAbility 参数name
    String name = intent.getStringParam("name");
    Text text = (Text)findComponentById(ResourceTable.Id_Text2);
    text.setText(name);



}

@Override
public void onActive() {
    super.onActive();
}

@Override
public void onForeground(Intent intent) {
    super.onForeground(intent);
}

}
3.background_button.xml
<?xml version=“1.0” encoding=“UTF-8” ?>
<shape xmlns:ohos=“http://schemas.huawei.com/res/ohos
ohos:shape=“rectangle”>
<corners
ohos:radius=“100”
/>
<solid
ohos:color=“#007dff”/>
</shape>
4.ability_main.xml
<?xml version=“1.0” encoding=“utf-8”?>
<DirectionalLayout
xmlns:ohos=“http://schemas.huawei.com/res/ohos
ohos:height=“match_parent”
ohos:width=“match_parent”
ohos:alignment=“center”
ohos:orientation=“vertical”>

<Text
    ohos:id="$+id:text"
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:background_element="$graphic:background_ability_main"
    ohos:layout_alignment="horizontal_center"
    ohos:text="$string:mainability_HelloWorld"
    ohos:text_size="40vp"
    />
<Button
    ohos:id="$+id:btn"
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:text="点击"
    ohos:text_size="19vp"
    ohos:text_color="#ffffff"
   ohos:top_padding="8vp"
    ohos:bottom_padding="8vp"
    ohos:left_padding="70vp"
    ohos:right_padding="70vp"
    ohos:margin="10vp"
    ohos:background_element="$graphic:background_button"

/>

</DirectionalLayout>
5.ability_second.xml
<?xml version=“1.0” encoding=“utf-8”?>
<DirectionalLayout
xmlns:ohos=“http://schemas.huawei.com/res/ohos
ohos:height=“match_parent”
ohos:width=“match_parent”
ohos:alignment=“center”
ohos:orientation=“vertical”>

<Text
    ohos:id="$+id:Text2"
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:background_element="$graphic:background_ability_main"
    ohos:layout_alignment="horizontal_center"
    ohos:text_size="20vp"/>

</DirectionalLayout>
Harmony OS 开发入门HelloWorld-鸿蒙开发者社区Harmony OS 开发入门HelloWorld-鸿蒙开发者社区Harmony OS 开发入门HelloWorld-鸿蒙开发者社区Harmony OS 开发入门HelloWorld-鸿蒙开发者社区

标签
app_Java.rar 1.2M 68次下载
5
收藏 3
回复
举报
2条回复
按时间正序
/
按时间倒序
SummerRic
SummerRic

“HelloWorld”

回复
2021-10-26 15:16:02
物联风景
物联风景

这算啥,代码搬运吗,你的理解呢,你的思考呢

回复
2021-10-27 10:23:09
回复
    相关推荐