EasyClick 原生UI连载二十九

EasyClick 原生UI连载目录

  • EasyClick 原生UI教程
  • EasyClick 原生UI连载二十九对话框交互
    • 效果图
    • XML 对话框布局
    • main.js 重点代码
    • ui.js 代码

EasyClick 原生UI教程

讲师:Mr-老鬼,QQ:1156346325
EasyClick 原生UI教程电梯直达
EasyClick 原生UI教程总纲

EasyClick 原生UI连载二十九对话框交互

效果图

EasyClick 原生UI连载二十九_第1张图片

XML 对话框布局



<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-老鬼 ~转载请注明原文地址
免责声明:本文所有的教程仅限交流学习使用不得用于违法用途,造成的法律后果本人不承担责任。

你可能感兴趣的:(Easy,Click,原生,UI,系列,android)