Ability Form,即表单,是 Page 形态的 Ability 的一种界面展示形式,用于嵌入到其他应用中作为其界面的一部分显示,并支持基础的交互功能。表单使用方作为表单展示的宿主负责显示表单,表单使用方的典型应用就是桌面。下图展示一种音乐播放应用 Page 的完整显示及其微缩展示效果。
图1 Page 及其表单
表单提供方是一个 Page 形态的 Ability,需要实现 onCreateForm() 方法,并返回一个 AbilityForm 对象。创建 AbilityForm 对象时需要指定表单布局文件。
<?xml version="1.0" encoding="utf-8"?>
<DirectionLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:width="match_parent"
ohos:height="match_parent"
ohos:orientation="horizontal">
<Text
ohos:id="$+id:text01"
ohos:width="match_content"
ohos:height="match_content"
ohos:text="Counter"
ohos:text_color="#FF555555"
ohos:text_size="20fp"/>
<Text
ohos:id="$+id:text02"
ohos:width="match_content"
ohos:height="match_content"
ohos:text_color="#FF0000FF"
ohos:text_size="20fp"/>
</DirectionLayout>
public class FormAbility extends Ability {
private static AbilityForm abilityForm;
private static int clickTimes = 0;
...
@Override
public AbilityForm onCreateForm() {
abilityForm = new AbilityForm(ResourceTable.Layout_form_layout, this);
abilityForm.setText(ResourceTable.Id_text02, generateFormText());
abilityForm.registerViewListener(ResourceTable.Id_text02, new OnClickListener() {
@Override
public void onClick(int viewId, AbilityForm form, ViewsStatus viewsStatus) {
clickTimes++;
form.setText(viewId, generateFormText());
if (FormSlice.text != null) {
FormSlice.text.setText("Client.Counter: " + clickTimes);
}
}
});
return abilityForm;
}
private static String generateFormText() {
return "total: " + clickTimes;
}
}
public class FormSlice extends AbilitySlice {
public static Text text;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
PositionLayout positionLayout = new PositionLayout(this);
ShapeElement background = new ShapeElement();
background.setShape(ShapeElement.RECTANGLE);
background.setRgbColor(new RgbColor(0xFFFFFFFF));
positionLayout.setBackground(background);
text = new Text(this);
text.setTextSize(30);
text.setTop(400);
text.setLeft(200);
text.setText(assembleText(FormAbility.getClickTimes()));
positionLayout.addComponent(text);
super.setUIContent(positionLayout);
}
...
}
{
"module": {
...
"abilities": [
{
...
"form-enabled": true,
"form": {
"default-height": 200,
"default-width": 300,
"min-height": 60,
"min-width": 80
},
...
}
]
...
}
...
}
说明
由于当前暂未支持 Form 缩放,即无法调整其显示尺寸,因此 min-height 和 min-width 字段暂无实际用途,可以省略。
表单使用方通常是桌面类应用。以下示例展示如何获取并展示表单。
"reqPermissions": [
{
"name": "ohos.permission.REQUIRE_FORM"
}
]
private void acquireForm() throws RemoteException {
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName("ohos.formsupplier.ability")
.withAbilityName("ohos.formsupplier.ability.FormAbility")
.build();
intent.setOperation(operation);
// Get form size.
formAbilityInfo = getBundleManager().queryAbilityByIntent(intent,IBundleManager.GET_ABILITY_INFO_WITH_PERMISSION,userId).get(0);
this.acquireAbilityFormAsync(intent, new AbilityForm.OnAcquiredCallback() {
@Override
public void onAcquired(AbilityForm abilityForm) {
// store as a class field
form = abilityForm;
}
@Override
public void onDestroyed(AbilityForm abilityForm) {
form = null;
}
});
}
private PositionLayout createFormHostLayout() {
PositionLayout formLayout = ...;
...
// set size same with default of form
formLayout.setHeight(formAbilityInfo.getDefaultFormHeight());
formLayout.setWidth(formAbilityInfo.getDefaultFormWidth());
...
return formLayout;
}
private void showForm() {
PositionLayout hostLayout = createFormHostLayout();
// attach form to host layout
hostLayout.addComponent(form.getView());
// attach host layout to root layout
rootLayout.addComponent(hostLayout);
}
如果开发者需要移除表单,调用 AbilitySlice 类的 releaseAbilityForm() 方法,并以此前获取的 AbilityFor 对象作为参数。
实现标题和文本区域最常用的是基础组件 text。text 组件用于展示文本,可以设置不同的属性和样式,文本内容需要写在标签内容区,...
场景介绍图像属性解码就是获取图像中包含的属性信息,比如EXIF属性。接口说明图像属性解码的功能主要由 ImageSource 和 ExifUtil...
场景介绍WLAN P2P 功能用于设备与设备之间的点对点数据传输,应用可以通过接口完成以下功能:发现对端设备。建立与移除群组。向...
FlexLayoutjava.lang.Object |---ohos.agp.components.Component |---|---ohos.agp.components.ComponentContainer |---|---|---...