EasyClick 原生UI連載目錄
- EasyClick 原生UI教程
- EasyClick 原生UI連載二十九對話框互動
- 效果圖
- XML 對話框布局
- main.js 重點代碼
- ui.js 代碼
EasyClick 原生UI教程
講師:Mr-老鬼,QQ:1156346325
EasyClick 原生UI教程電梯直達:
EasyClick 原生UI教程總綱
EasyClick 原生UI連載二十九對話框互動
效果圖

XML 對話框布局
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright(c) 2021,
~ 專案名稱:對話框互動
~ 檔案名稱:dialogview.xml
~ 創建時間:2021/4/29 下午4:08
~ 作者:laogui
-->
<LinearLayout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:android="http://schemas.android.com/apk/res/android"
xsi:noNamespaceSchemaLocation="layout.xsd"
android:layout_width="220dp"
android:layout_height="150dp"
android:orientation="horizontal"
android:layout_margin="2dp"
android:background="#FFFFFFFF">
<View android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#0080FF" />
<LinearLayout android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0080FF"
android:orientation="horizontal">
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="輸入資訊對話框"
android:textColor="#FFFFFF"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:orientation="vertical">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="請輸入內容"
android:textColor="#ff0000" />
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:tag="editText"
android:textColor="#000000"
android:hint="請輸入內容" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#0080FF"
android:orientation="horizontal"
android:padding="8dp">
<TextView android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0080FF"
android:gravity="center"
android:tag="okBtn"
android:text="確定"
android:textColor="#FFFFFF"
android:textSize="10sp" />
<View android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#FFFFFF" />
<TextView android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0080FF"
android:gravity="center"
android:text="取消"
android:textColor="#FFFFFF"
android:textSize="10sp"
android:tag="closeBtn" />
</LinearLayout>
</LinearLayout>
<View android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#0080FF" />
</LinearLayout>
main.js 重點代碼
/*
* Copyright(c) 2021,
* 專案名稱:對話框互動
* 檔案名稱:main.js
* 創建時間:2021/4/29 下午4:07
* 作者:laogui
*/
let edistr = "";
function main() {
//開始再這里撰寫代碼了!!
//如果自動化服務正常
if (!autoServiceStart(3)) {
logd("自動化服務啟動失敗,無法執行腳本")
exit();
return;
}
logd("開始執行腳本...")
//home();
laoguiDialog();
let b = 100;
// 模擬腳本運行場景,,,,,,
while (b <= 100) {
sleep(1000);
b--;
logd("模擬等待接收對話框資訊:"+edistr);
}
}
/**
* XML自定義對話框函式
* 按鈕互動
* 編輯框互動
* 不需要編輯框的自己改成textview控制元件做提示框
*/
function laoguiDialog() {
// 匯入必要的Android包
importPackage(android.widget);
importPackage(android.graphics);
importPackage(android.view);
importPackage(android.app);
// 決議對話框 xml布局
let dialogView = ui.parseView("dialogview.xml");
// 加載對話框并顯示
ui.customDialog({
"fullScreen": false, // 設定為非全屏
"cancelable": true // 可以取消
},
// 這是決議的對話框物件
dialogView,
// 這里是各種事件操作
function (dialog, v) {
//設定各種事件 省略,,,
// 這里是獲取對話框布局的所有要控制的view物件,根據定義的 tag 獲取
let okBtn = ui.findViewByTag("okBtn")
let closeBtn = ui.findViewByTag("closeBtn")
let detTV = ui.findViewByTag("editText")
let myDialog = dialog;
// 確定按鈕, 獲取編輯框內容并保存配置
okBtn.setOnClickListener(function (v) {
let str = detTV.getText();// 獲取編輯框文本字串
logd(str);
ui.saveAllConfig();// 保存配置
logd("點擊了");
// 必須執行緒去操作對話框
thread.execAsync(function () {
myDialog.doDismiss();// 關閉對話框
})
toast("輸入的資訊:" + str);
edistr = str;
})
// 取消按鈕,關閉對話框
closeBtn.setOnClickListener(function (v) {
logd("取消了");
detTV.setText("");
ui.saveAllConfig();
edistr = "";
// 必須執行緒去操作對話框
thread.execAsync(function () {
myDialog.doDismiss();
})
})
}, function () {
logd("對話框關閉了");
// 必須執行緒去操作對話框
thread.execAsync(function (v) {
detTV.setText("");
ui.saveAllConfig();
})
})
}
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();
ui.js 代碼
/*
* Copyright(c) 2021,
* 專案名稱:對話框互動
* 檔案名稱:ui.js
* 創建時間:2021/4/29 下午4:07
* 作者:laogui
*/
function main() {
ui.layout("引數設定", "main.xml");
}
main();
詳細的代碼注釋已經寫進代碼了,就不再詳解原理了,
我是Mr-老鬼、QQ1156346325 ,交流QQ群:620028786,647082990
------------------------------------------------著作權宣告------------------------------------------------------
本文著作權所有~Mr-老鬼 ~轉載請注明原文地址
免責宣告:本文所有的教程僅限交流學習使用不得用于違法用途,造成的法律后果本人不承擔責任,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/281662.html
標籤:其他
