EasyClick 原生UI连载六

EasyClick 原生UI连载目录

    • EasyClick 原生UI教程
    • 原生UI连载六UI与脚本之间的交互 一
      • 项目文件介绍
      • UI 布局文件main.xml
      • UI入口文件 ui.js
      • 更多控件设置方式
    • 重要提示

EasyClick 原生UI教程

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

原生UI连载六UI与脚本之间的交互 一

了解下兄弟篇:原生UI连载二十六UI与脚本之间的交互 二

项目文件介绍

本篇讲解UI 如何与脚本进行交互
图中说明:

  1. main.js 主脚本入口
  2. ui.js UI界面运行入口
  3. main.xml UI界面文件

EasyClick 原生UI连载六_第1张图片

UI 布局文件main.xml

<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">
        <LinearLayout android:orientation="horizontal"
      				  android:layout_height="match_parent" 
    				  android:layout_width="match_parent">
            <Button 
           			android:layout_height="match_parent" 
    				android:layout_width="0dp"
            		android:text="我是测试按钮"
            		android:layout_weight="1"
            		android:tag="btn"/>
        LinearLayout>
    LinearLayout>
ScrollView>

UI入口文件 ui.js

function main() {
     
    ui.layout("参数设置", "main.xml");
    //  UI 的main.xml 里面有个按钮控件 这里来操作下这个按钮
    ui.setEvent(ui.btn, "click", function (view) {
     // 使用ui.setEvent(控件tag,操作,事件函数)函数来操作按钮控件
        ui.start();// 启动脚本
    });
}

main();

再编写脚本入口文件 main.js

function main() {
     
    //开始再这里编写代码了!!
    //如果自动化服务正常
    if (!autoServiceStart(3)) {
     
        logd("自动化服务启动失败,无法执行脚本")
        exit();
        return;
    }
    logd("开始执行脚本...")
    logd(device.getScreenWidth()+"    "+ device.getScreenHeight());//获取屏幕宽高并打印
}
/**
* 这个函数是检测自动服务的 
*/
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();

更多控件设置方式

    //Switch 开关按钮的用法
    var auto_env = ui.getViewValue(ui.auto_env);
    ui.logd("tag为 auto_env 的值: " + auto_env);
    //开关按钮的事件
    ui.setEvent(ui.auto_env, "checkedChange", function (view, isChecked) {
     
        ui.logd("tag为 auto_env isChecked " + isChecked);
        if (isChecked) {
     
            startAutoEnv();
        }
    });
    if (ui.isServiceOk()) {
     
        ui.auto_env.setChecked(true);//设置开关为开状态
    } else {
     
        ui.auto_env.setChecked(false);//设置开关为关状态
    }
    //EditText 编辑框的用法
    var name = ui.getViewValue(ui.name);
    ui.logd("tag为name的值: " + name);
    ui.name.setText("我是name的值"); //设置编辑框内容
    //Spinner 下拉选择框用法
    var sex = ui.getViewValue(ui.sex); 
    ui.logd("tag为 sex 的值: " + sex);
    //下拉选择框的事件
    ui.setEvent(ui.sex, "itemSelected", function (position, value) {
     
        ui.logd("tag为 sex itemSelected " + value);
    });

    //RadioButton 单选按钮用法
    var three = ui.getViewValue(ui.three);  //获取单选按钮的值
    ui.logd("tag为 three 的值: " + three);
    //单选按钮的事件
    ui.setEvent(ui.three, "checkedChange", function (view, isChecked) {
     
        ui.logd("tag为 three isChecked " + isChecked);
    });
    //CheckBox 复选框用法
    var dance = ui.getViewValue(ui.dance); //获取复选框的值
    ui.logd("tag为 dance 的值: " + dance);
    //复选框的事件
    ui.setEvent(ui.dance, "checkedChange", function (view, isChecked) {
     
        ui.logd("tag为 dance isChecked " + isChecked);
        // 复选框状态改变这里保存改变后的值
        ui.saveAllConfig(); //保存所有配置
    });

    //saveAllBtn 保存参数事件
    ui.setEvent(ui.saveAllBtn, "click", function (view) {
     
        var s = ui.saveAllConfig();// 保存所有配置
        ui.logd("保存所有参数结果 " + s)
    });

    //系统设置按钮
    ui.setEvent(ui.systemSetting, "click", function (view) {
     
        ui.openECSystemSetting();

    });
    //启动脚本按钮
    ui.setEvent(ui.startBtn, "click", function (view) {
     
        ui.start();
    });
    //启动环境按钮
    ui.setEvent(ui.envBtn, "click", function (view) {
     
        //异步启动环境,如果成功了就设置auto_env 按钮的状态
        startAutoEnv();
    });
    //获取所有的UI参数
    ui.logd("获取所有的UI参数:" + ui.getConfigJSON());

重要提示

UI里的配置只加载一次修改后务必保存!!!

我是Mr-老鬼、QQ1156346325 。交流QQ群:620028786,647082990
---------------------------------------版权声明------------------------------------------------------
版权所有~Mr-老鬼 ~转载请注明原文地址。
免责声明:本文所有的教程仅限交流学习使用不得用于违法用途,造成的法律后果本人不承担责任。

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