import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.AbilitySliceLifecycleExecutor;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
public class MyAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
// 创建一个文本组件
Text text = (Text) findComponentById(ResourceTable.Id_text);
text.setText("Hello, HarmonyOS!");
// 创建一个按钮组件
Button button = (Button) findComponentById(ResourceTable.Id_button);
button.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
text.setText("Button Clicked!");
}
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
@Override
public void onBackground() {
super.onBackground();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onFormResult(Intent intent) {
super.onFormResult(intent);
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
@Override
public void onCommand(Intent intent, boolean restart, int startId) {
super.onCommand(intent, restart, startId);
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
public void onAbilityResult(int requestCode, int resultCode, Intent resultIntent) {
super.onAbilityResult(requestCode, resultCode, resultIntent);
}
@Override
public void onCompleteContinuation(int token) {
super.onCompleteContinuation(token);
}
@Override
public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
}
}
在上面的代码中,创建了一个名为MyAbilitySlice
的能力分片。在onStart
方法中,使用setUIContent
方法设置了布局文件ability_main.xml
作为该分片的用户界面。然后,获取到界面上的文本组件和按钮组件,并进行了相应的设置和事件监听。
目前鸿蒙系统上的BS开发方面支持相对较少,但随着鸿蒙生态的不断完善,相信会有更多BS开发方面的支持和框架出现。