美文网首页
android MotionEvent.obtain模拟事件,自

android MotionEvent.obtain模拟事件,自

作者: 因为我的心 | 来源:发表于2021-10-06 09:48 被阅读0次

一、前言:

模拟点击事件主要用到第三方SDK中,我们获取不到页面子控件的点击事件 ,我们又想模拟点击,跳转到详情页面中,这时候使用模拟点击事件是最好不过的了。应用最多的比如说广告SDK。

二、使用:

1、MainActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        /**
         * btn点击按钮A
         */
        btn.setOnClickListener {
            clickByMistake()
        }

        /**
         * 重写btn2的setOnTouchListener
         */
        btn2.setOnTouchListener(OnTouchListener { v, event ->
            val toast = Toast.makeText(
                applicationContext,
                "欢迎点击了我啊",
                Toast.LENGTH_LONG
            )
            toast.show()
            true
        })
    }

    /**
     * 执行模拟点击事件
     */
    private fun clickByMistake() {
        // Obtain MotionEvent object
        val downTime = SystemClock.uptimeMillis()
        val eventTime = SystemClock.uptimeMillis() + 100
        val x = 0.0f
        val y = 0.0f
        // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
        val metaState = 0
        val motionEvent = MotionEvent.obtain(
            downTime,
            eventTime,
            MotionEvent.ACTION_UP,
            x,
            y,
            metaState
        )

        //btn2发送触摸事件以查看
        btn2.dispatchTouchEvent(motionEvent)
        // 确保recycle方法只调用一次
        motionEvent.recycle()
    }
}

2、activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是第一个页面"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textColor="#f00"
        />


    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints"
        android:text="点击按钮A"
        />

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints"
        android:text="欢迎点击了我啊"
        />
</LinearLayout>

3、代码介绍:

1、点击btn的时候,调用clickByMistake()方法,里面重写btn2的dispatchTouchEvent方法(motionEvent)==》(相当于点击了btn2);
2、点击btn2,可以直接弹Toast;

4、参考大佬示例:

 private fun setSimulateClick(view: View, x: Float, y: Float) {
            var downTime = SystemClock.uptimeMillis()
            val downEvent = MotionEvent.obtain(
                downTime, downTime,
                MotionEvent.ACTION_DOWN, x, y, 0
            )
            downTime += 1000;
            val upEvent = MotionEvent.obtain(
                downTime, downTime,
                MotionEvent.ACTION_UP, x, y, 0
            )
            view.onTouchEvent(downEvent)
            view.onTouchEvent(upEvent)
            downEvent.recycle()
            upEvent.recycle()
        }

5、学习MotionEvent.obtain参考以下链接:

相关文章

  • android MotionEvent.obtain模拟事件,自

    一、前言: 模拟点击事件主要用到第三方SDK中,我们获取不到页面子控件的点击事件 ,我们又想模拟点击,跳转到详情页...

  • button.performClick();

    转自Android中performClick方法---代码调用点击事件(模拟去触摸控件) - CSDN博客 最近看...

  • 自动化工具-ADB

    ADB命令在手机端的自动化控制和处理 1. Android adb 模拟滑动 按键 点击事件 模拟事件全部是通过i...

  • Android输入事件模拟

    在一些自动化测试等情景下,输入Android应用程序产生一些屏幕点击等的输入事件以实现特定需求。本文总结了几种An...

  • Android 模拟发送事件

    所有的事件一览 一、TrackBall 事件 定义:等同于连续点击左右按键。格式: roll ...

  • Android 模拟键盘事件

    开发中,遇到了需要模拟键盘事件的问题。在这里做一个简单总结。使用模拟键盘事件,需要知道对用按键的对照表。参考这个博...

  • Android事件分发之基础

    转载自Android事件分发之基础Android 事件分发机制是比较重要的一块内容,总结一下 Android 事件...

  • Android 模拟器现已支持 AMD 处理器和 Hyper-V

    作者:Jamal Eason, Android 产品经理 自两年前谷歌对 Android 模拟器进行重大更新以来,...

  • 安卓高级话题 - 收藏集 - 掘金

    图解 Android 事件分发机制 - Android - 掘金转自:http://www.jianshu.com...

  • Android下模拟点击事件

    要实现在WebView上模拟点击效果,效果一直不好,函数是搜索得到的,有几点需要注意: 1、按下和松开之间的时间要...

网友评论

      本文标题:android MotionEvent.obtain模拟事件,自

      本文链接:https://www.haomeiwen.com/subject/wwwenltx.html