Harmony OS 开发入门HelloWorld

发布于 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 52次下载
5
收藏 3
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐