安卓延迟自动点击

1. 自动点击

1.1. 调用View方法

直接调用view.performClick()即可实现单击;调用view.performLongClick()即可实现长按。

但是对一些复杂的操作,View自带的方法可能不够用了,就需要用到第二种方法

1.2. 使用MotionEvent设置触摸事件

可以通过设置MotionEvent对象的参数,再通过View的dispatchTouchEvent分发点击事件,完成一些复杂的触摸操作。

MotionEvent.obtain()参数说明:

参数 说明
downTime 触发当前触摸事件的手指第一次按下的时间,从手指接触屏幕到手指离开屏幕的时间内,该手指产生的所有触摸事件,其downTime都不会发生改变
eventTime 当前触摸事件产生的时间。如果是第一次按下,则该时间与getDownTime相同
action 事件类型。如MotionEvent.ACTION_DOWN、MotionEvent.ACTION_MOVE、MotionEvent.ACTION_UP
x 事件的x坐标
y 事件的y坐标

说明:

downTime、eventTime这两个参数并不控制事件的触发时间,推测是用于处理触摸事件时获取一些数据,比如从按下到当前触摸事件的时间间隔。虽然注释说这两个都需要通过SystemClock.uptimeMillis()

获取,但实测发现,设置任意值都可以触发touch事件。

  1. 如果只是要实现点击,x、y设置任意值都可以,只要用对应的view去调用dispatchTouchEvent即可。

2. 延迟点击

我们希望实现的效果是:在延迟自动点击前,可以响应用户操作,延时时间到了以后,自动完成点击事件。

第一时间想到的是启动一个子线程,在子线程中sleep一段时间,然后进行点击,但是会有各种问题,详情如下:

2.1. TimerTask
java 复制代码
Timer timer = new Timer();
timer.

schedule(new TimerTask() {
    @Override
    public void run () {
        button.performClick();
    }
},3000L);

这种方法在delayMillis结束之前可以交互,但delayMillis结束之后自动点击会抛出异常:

  • 若使用button.performClick();
    ,则报错android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.;
  • 若使用MotionEvent,则报错android.util.AndroidRuntimeException: Animators may only be run on Looper threads
2.2. new Handler(Looper.getMainLooper()).post()
java 复制代码
new Handler(Looper.getMainLooper()).

post(() ->{
        try{
        Thread.

sleep(3000L);
            }catch(
InterruptedException e){
        throw new

RuntimeException(e);
            }
                    button.

performClick();
        }
                );

这种方法在delayMillis结束之前不可以交互,点击事件被累积,delayMillis结束之后自动点击正常,并响应之前积压的点击事件。

2.3. new Handler(Looper.getMainLooper()).postDelayed()
java 复制代码
new Handler(Looper.getMainLooper()).

postDelayed(() ->{
        button.

performClick();
},3000L);

这种方法在delayMillis结束之前可以交互,delayMillis结束之后自动点击正常,满足需求。

3. 完整示例

布局文件activity_auto_click.xml:

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center">

        <Button
                android:id="@+id/auto_click_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:text="-" />
    </LinearLayout>
</LinearLayout>

activity类:

java 复制代码
package org.tao.hetools.activities;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import androidx.activity.ComponentActivity;
import androidx.annotation.Nullable;

import org.tao.hetools.R;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AutoClickActivity extends ComponentActivity {
    private int index = 0;
    private Button button;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_auto_click);
        button = findViewById(R.id.auto_click_button);
        button.setText(String.valueOf(index));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        button.setOnClickListener(view -> {
            Log.i("AutoClick", "===>click once" + simpleDateFormat.format(new Date()));
            button.setText(String.valueOf(++index));
        });
        button.setOnLongClickListener(view -> {
            Log.i("AutoClick", "===>long click once" + simpleDateFormat.format(new Date()));
            int backgroundColor = ((ColorDrawable) button.getBackground()).getColor();
            button.setBackgroundColor(backgroundColor == Color.WHITE ? Color.BLUE : Color.WHITE);
            return true;
        });
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            // button.performClick(); // 单击
            // button.performLongClick(); // 长按
            clickOnce(button);
        }, 3000L);
    }

    public void clickOnce(View view) {
        int x = view.getWidth() / 2;
        int y = view.getHeight() / 2;
        long downTime = SystemClock.uptimeMillis();
        // 按下手指
        long actionDown = downTime;
        MotionEvent event = MotionEvent.obtain(downTime, actionDown, MotionEvent.ACTION_DOWN, x, y, 0);
        view.dispatchTouchEvent(event);
        // 抬起手指
        long actionUp = downTime + 500;
        event = MotionEvent.obtain(downTime, actionUp, MotionEvent.ACTION_UP, x, y, 0);
        view.dispatchTouchEvent(event);

        event.recycle();
    }
}
相关推荐
teacher伟大光荣且正确1 小时前
Qt Creator 配置 Android 编译环境
android·开发语言·qt
飞猿_SIR4 小时前
Android Exoplayer 实现多个音视频文件混合播放以及音轨切换
android·音视频
HumoChen994 小时前
GZip+Base64压缩字符串在ios上解压报错问题解决(安卓、PC模拟器正常)
android·小程序·uniapp·base64·gzip
沙振宇8 小时前
【HarmonyOS】ArkTS开发应用的横竖屏切换
android·华为·harmonyos
橙子1991101610 小时前
Kotlin 中的作用域函数
android·开发语言·kotlin
zimoyin10 小时前
Kotlin 懒初始化值
android·开发语言·kotlin
枣伊吕波11 小时前
第六节第二部分:抽象类的应用-模板方法设计模式
android·java·设计模式
萧然CS11 小时前
使用ADB命令操作Android的apk/aab包
android·adb
_extraordinary_15 小时前
MySQL 事务(二)
android·数据库·mysql
鸿蒙布道师19 小时前
鸿蒙NEXT开发动画案例5
android·ios·华为·harmonyos·鸿蒙系统·arkui·huawei