Auto.js Study Notes 17: Basic listening events and simple UI click event operations

Table of contents

events

Related parameters

code example

Register touch events

Delete all event listening functions

Button click event

Switch

button

timepicker

Summarize


Declare that the autojs I use is version 4.1.1

events

Enable key monitoring, such as volume keys and home key. Keystroke monitoring is implemented using the accessibility service. If the accessibility service is not enabled, an exception will be thrown and prompted to enable it.

Only after this function is successfully executed, the monitoring of key events such as onKeyDown, onKeyUp will be effective.

This function can only be used on Android 4.3 and above.

Related parameters

keys.home home key

keys.back return key

keys.menu menu keys

keys.volume_up volume up key

keys.volume_down volume down key

code example

//启用按键监听
events.observeKey();
//监听音量上键按下
events.onKeyDown("volume_up", function(event){
    toast("音量上");
    unregisterReceiver();
});

//监听音量下键弹起
events.onKeyDown("volume_down", function(event){
    toast("音量下");
});

//监听Home键弹起
events.onKeyDown("home", function(event){
    toast("Home");
});

//监听菜单键按下
events.onKeyDown("menu", function(event){
    toast("菜单键被按下了");
});

Register touch events

  • ​ listener {Function} function whose parameter is Point ​

Register a touch listening function. Equivalent toon("touch", listener).

events.observeTouch();
//注册触摸监听器
events.onTouch(function(s){
    //触摸事件发生会打印出触摸的坐标
    log(s.x + ", " + s.y);
});

Delete all event listening functions

events.removeAllTouchListeners()

Button click event

Switch

ui partial code

<vertical id = "setConfigView" w = "*" h = "auto" layout_centerInParent="true" >
      <Switch id = "openSwh"  w = "auto" h = "auto" textStyle = "bold" textColor = "red" 
           text = "无障碍权限   "  textSize="16sp" marginBottom = "15"  marginRight ="10"/>
</vertical>

Click event code

 //按钮监听方式
    ui.openSwh.on("check", function(checked) {
        // 用户勾选无障碍服务的选项时,跳转到页面让用户去开启
        if(checked && auto.service == null) {
            app.startActivity({
                action: "android.settings.ACCESSIBILITY_SETTINGS"
            });
            exit();
        }
        if(!checked && auto.service != null){
            auto.service.disableSelf(); //关闭无障碍服务
            ui.openSwh.setChecked(false);
        }
    });

button

ui partial code

<button id = "testServerAPI" align="center">测试接口数据</button>

Click event code


ui.testServerAPI.click(()=>{
        testAPIState = true;
        getServiceInfo(); 
});

timepicker

uiyoto

<text id = "timePickerModeText" text = "滑动时间选择:" textColor = "black" textSize="16sp" marginTop="5" />
<timepicker id = "timePickerMode"  timePickerMode="spinner" />

Listening event code

//滑动时间选择
ui.timePickerMode.setIs24HourView(true);//设置当前时间控件为24小时制
ui.timePickerMode.setHour(9);   //设置当前小时
ui.timePickerMode.setMinute(0); //设置当前分(0-59)
ui.timePickerMode.setOnTimeChangedListener({
      onTimeChanged: function (v, h, m) {
      //h 获取的值 为24小时格式
      wxAutoConfigObj.timingTime = h + ":" + m ;
      ui.timePickerModeText.setText("滑动时间选择: " + wxAutoConfigObj.timingTime);
   }
});

Summarize

Just remember, the above are common event processing, learn a little bit every day.

Just watching and typing is useless
After reading, you must practice it
You must type the code
Be sure to run trial and error
This is meaningful learning

Guess you like

Origin blog.csdn.net/piyangbo/article/details/124946639