【Serverless】快速集成云函数HarmonyOS( 三 )


5.         将详细信息中的触发URL的后缀保存,作为客户端请求时的触发器标识 。

【Serverless】快速集成云函数HarmonyOS

文章插图
?
6、界面设计本次Codelab中您可以在DevEco Studio工程中创建一个布局页面,参照下图进行UI设计,具备两个数字的输入,云函数返回求和结果的展示功能即可 。
【Serverless】快速集成云函数HarmonyOS

文章插图
?
布局文件代码如下:
<?xml version="1.0" encoding="utf-8"?><DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:alignment="center"ohos:orientation="vertical"><DirectionalLayoutohos:width="match_parent"ohos:height="50vp"ohos:alignment="center"ohos:orientation="horizontal"><TextFieldohos:id="$+id:editText_number1"ohos:width="120vp"ohos:height="match_content"ohos:hint="$string:mainability_input_number"ohos:max_text_lines="1"ohos:text_size="20vp"/></DirectionalLayout><DirectionalLayoutohos:width="match_parent"ohos:height="50vp"ohos:alignment="center"ohos:orientation="horizontal"><TextFieldohos:id="$+id:editText_number2"ohos:width="120vp"ohos:height="match_content"ohos:hint="$string:mainability_input_number"ohos:max_text_lines="1"ohos:text_size="20vp" /></DirectionalLayout><DirectionalLayoutohos:width="match_parent"ohos:height="50vp"ohos:alignment="center"ohos:orientation="horizontal"><Buttonohos:id="$+id:btn_add"ohos:width="match_content"ohos:height="match_content"ohos:text_size="20vp"ohos:text="$string:mainability_add_number"/></DirectionalLayout><DirectionalLayoutohos:width="match_parent"ohos:height="50vp"ohos:alignment="center"ohos:orientation="horizontal"><Textohos:width="match_content"ohos:height="match_content"ohos:text="$string:mainability_number_res"ohos:text_size="20vp" /><Textohos:id="$+id:textView_result"ohos:width="match_content"ohos:height="match_content"ohos:text_size="20vp"/></DirectionalLayout></DirectionalLayout>
【Serverless】快速集成云函数HarmonyOS

文章插图
7、云函数开发1.         在应用启动的onStart方法中获取控件实例并设置按钮的点击事件 。
@Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);textFieldNum1 = (TextField)findComponentById(ResourceTable.Id_editText_number1);textFieldNum2 = (TextField)findComponentById(ResourceTable.Id_editText_number2);textView = (Text)findComponentById(ResourceTable.Id_textView_result);Button btn_add = (Button)findComponentById(ResourceTable.Id_btn_add);btn_add.setClickedListener(listener -> testFunctionAdd());}
【Serverless】快速集成云函数HarmonyOS

文章插图
2.         初始化云函数服务 。
AGConnectFunction function = AGConnectFunction.getInstance();
【Serverless】快速集成云函数HarmonyOS

文章插图
3.         生成云函数所需要的事件的map对象,这里因为我们的云函数中设置的事件的key值为“number1”和“number2”,所以我们设置map的key值为“number1”和“number2” 。
HashMap<String, Integer> number = new HashMap();number.put("number1", Integer.parseInt(textFieldNum1.getText()));number.put("number2", Integer.parseInt(textFieldNum2.getText()));
【Serverless】快速集成云函数HarmonyOS

经验总结扩展阅读