原生UI連載目錄
- EasyClick 原生UI教程
- 原生UI連載二十六UI與腳本之間的互動 二
- UI 引數設定效果圖
- main.xml 代碼
- ui.js代碼
- main.js代碼
- 重要提示
EasyClick 原生UI教程
講師:Mr-老鬼,QQ:1156346325
EasyClick 原生UI教程電梯直達:EasyClick 原生UI教程總綱
原生UI連載二十六UI與腳本之間的互動 二
了解下兄弟篇:原生UI連載六UI與腳本之間的互動 一
UI 引數設定效果圖

main.xml 代碼
<?xml version="1.0" encoding="UTF-8" ?><!--
~ Copyright(c) 2021,
~ 檔案名稱:main.xml
~ 創建時間:2021/4/25 下午9:50
~ 作者:laogui
-->
<ScrollView xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:android="http://schemas.android.com/apk/res/android"
xsi:noNamespaceSchemaLocation="layout.xsd"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="20dp">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="表單開始,設定tag屬性,用于在代碼里面獲取對應的值" />
<LinearLayout android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_width="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名: " />
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="name"
android:hint="請輸入姓名" />
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_width="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年齡: " />
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="age"
android:hint="請輸入年齡" />
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_width="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性別: " />
<Spinner android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="sex"
android:text="男同學|女同學" />
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_width="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜歡玩" />
<EditText android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:tag="a1"
android:hint="什么" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="和" />
<EditText android:layout_width="100dp"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:tag="a2"
android:hint="什么" />
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_width="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="愛 好: " />
<LinearLayout android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_width="match_parent">
<CheckBox android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="music"
android:text="聽音樂" />
<CheckBox android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="sing"
android:text="唱歌" />
<CheckBox android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="dance"
android:text="跳舞" />
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_width="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="所在年級: " />
<RadioGroup android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_width="match_parent">
<RadioButton android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="one"
android:text="一年級" />
<RadioButton android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="two"
android:text="二年級" />
<RadioButton android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="three"
android:text="三年級" />
</RadioGroup>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_width="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="備注: " />
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="mark"
android:minHeight="100dp"
android:maxLines="1000"
android:hint="備注" />
</LinearLayout>
<Button android:layout_width="match_parent"
android:layout_height="40dp"
android:tag="saved"
android:text="保存資料" />
</LinearLayout>
</ScrollView>
ui.js代碼
/*
* Copyright(c) 2021,
* 檔案名稱:ui.js
* 創建時間:2021/4/25 下午9:50
* 作者:laogui
*/
function main() {
ui.layout("引數設定", "main.xml");
ui.setEvent(ui.saved,"click",function (view){
let saved = ui.saveAllConfig();// 保存所有資料
logd(saved);
})
}
main();
點擊按鈕保存資料除錯結果
[2021-04-25 22:02:05.265]【遠程設備】預覽成功...
[2021-04-25 22:02:05.597]【遠程設備】決議UI layout/ui.js檔案
[2021-04-25 22:02:05.938]【遠程設備】執行UI成功
[2021-04-25 22:02:39.205]【遠程設備】true // 資料回傳保存成功
main.js代碼
/*
* Copyright(c) 2021,
* 專案名稱:619
* 檔案名稱:main.js
* 創建時間:2021/4/25 下午9:50
* 作者:laogui
*/
function main() {
//開始再這里撰寫代碼了!!
toast("Hello World");
//如果自動化服務正常
if (!autoServiceStart(3)) {
logd("自動化服務啟動失敗,無法執行腳本")
exit();
return;
}
logd("開始執行腳本...")
let name = readConfigString("name");//讀取單個配置
logd("姓名: " + name);
logd("年齡: " + readConfigString("age"));
logd("聽音樂: " + readConfigString("music"));
logd("是不是一年級: " + readConfigString("one"));
logd("備注: " + readConfigString("mark"));
logd(JSON.stringify(getConfigJSON()));// 獲取所有配置
}
function autoServiceStart(time) {
for (var i = 0; i < time; i++) {
if (isServiceOk()) {
return true;
}
var started = startEnv();
logd("第" + (i + 1) + "次啟動服務結果: " + started);
if (isServiceOk()) {
return true;
}
}
return isServiceOk();
}
main();
運行工程除錯結果
[2021-04-25 22:03:37.665]【遠程設備】準備運行工程...
[2021-04-25 22:03:37.668]【遠程設備】開始啟動...
[2021-04-25 22:03:37.758]【遠程設備】開始執行,請稍等
[2021-04-25 22:03:37.779]【遠程設備】決議js/main.js檔案
[2021-04-25 22:03:37.797]【遠程設備】Toast訊息: (js/main.js#12 )Hello World
[2021-04-25 22:03:47.58]【遠程設備】第1次啟動服務結果: true
[2021-04-25 22:03:47.76]【遠程設備】開始執行腳本...
[2021-04-25 22:03:47.78]【遠程設備】姓名: 張文
[2021-04-25 22:03:47.82]【遠程設備】年齡: 18
[2021-04-25 22:03:47.86]【遠程設備】{"name":"張文","age":"18","sex":"女同學","a1":"跳舞","a2":"看書","music":true,"sing":true,"dance":true,"one":false,"two":false,"three":true,"mark":"小姐姐","saved":"保存資料","jobTaskTag":""}
[2021-04-25 22:03:47.88]【遠程設備】聽音樂: true
[2021-04-25 22:03:47.90]【遠程設備】備注: 小姐姐
[2021-04-25 22:03:47.91]【遠程設備】是不是一年級: false
重要提示
注意XML檔案里的tag屬性 和ui.saved這種一定是對應的否則會報錯!!!
我是Mr-老鬼、QQ1156346325 ,交流QQ群:620028786,647082990
------------------------------------------------著作權宣告------------------------------------------------------
本文著作權所有~Mr-老鬼 ~轉載請注明原文地址
免責宣告:本文所有的教程僅限交流學習使用不得用于違法用途,造成的法律后果本人不承擔責任,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/280220.html
標籤:其他
上一篇:uni-app,攜帶cookie(用戶后臺shiro的認證)
下一篇:【Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)】-B.Morning Jogging-貪心+模擬
