GVKun编程网logo

android 模拟用户点击事件(安卓模拟用户点击)

21

在本文中,我们将给您介绍关于android模拟用户点击事件的详细内容,并且为您解答安卓模拟用户点击的相关问题,此外,我们还将为您提供关于Androidadb模拟滑动按键点击事件、AndroidButt

在本文中,我们将给您介绍关于android 模拟用户点击事件的详细内容,并且为您解答安卓模拟用户点击的相关问题,此外,我们还将为您提供关于Android adb 模拟滑动 按键 点击事件、Android Button 四种点击事件和长按事件、android listview 点击事件失效、Android ScrollView 滑动事件和子控件点击事件冲突的知识。

本文目录一览:

android 模拟用户点击事件(安卓模拟用户点击)

android 模拟用户点击事件(安卓模拟用户点击)

模拟用户点击事件

   private void setSimulateClick(View view, float x, float y) {
        long downTime = SystemClock.uptimeMillis();
        final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime,MotionEvent.ACTION_DOWN, x, y, 0);
        downTime += 1000;
        final MotionEvent upEvent = MotionEvent.obtain(downTime, downTime,MotionEvent.ACTION_UP, x, y, 0);
        view.onTouchEvent(downEvent);view.onTouchEvent(upEvent);downEvent.recycle();
        upEvent.recycle();}

 

Android adb 模拟滑动 按键 点击事件

Android adb 模拟滑动 按键 点击事件

模拟事件全部是通过 input 命令来实现的,首先看一下 input 命令的使用: 

 

usage: input ...

 

       input text <string>

       input keyevent <key code number or name>

       input tap <x> <y>

       input swipe <x1> <y1> <x2> <y2>

 

1. keyevent 指的是 android 对应的 keycode,比如 home 键的 keycode=3,back 键的 keycode=4.

 

具体请查阅 <android keycode 详解> http://blog.csdn.net/huiguixian/article/details/8550170

 

然后使用的话比较简单,比如想模拟 home 按键:

 

adb shell input keyevent 3

 

请查阅上述文章,根据具体 keycode 编辑即可。

 

 

2. 关于 tap 的话,他模拟的是 touch 屏幕的事件,只需给出 x、y 坐标即可。

 

此 x、y 坐标对应的是真实的屏幕分辨率,所以要根据具体手机具体看,比如你想点击屏幕 (x, y) = (250, 250) 位置:

 

adb shell input tap 250 250

 

 

3. 关于 swipe 同 tap 是一样的,只是他是模拟滑动的事件,给出起点和终点的坐标即可。例如从屏幕 (250, 250), 到屏幕 (300, 300) 即

 

adb shell input swipe 250 250 300 300


Android Button 四种点击事件和长按事件

Android Button 四种点击事件和长按事件

项目 XML 代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation="vertical"
 8     tools:context=".MainActivity">
 9 
10     <Button
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:layout_marginTop="10dp"
14         android:onClick="doClick"
15         android:text="xml添加doClock"/>
16 
17     <Button
18         android:id="@+id/button2"
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:layout_marginTop="10dp"
22         android:text="匿名内部类点击"/>
23 
24     <Button
25         android:id="@+id/button3"
26         android:layout_width="match_parent"
27         android:layout_height="wrap_content"
28         android:layout_marginTop="10dp"
29         android:text="自定义点击事件"/>
30 
31     <Button
32         android:id="@+id/button4"
33         android:layout_width="match_parent"
34         android:layout_height="wrap_content"
35         android:layout_marginTop="10dp"
36         android:text="继承方式"/>
37 
38     <Button
39         android:id="@+id/button5"
40         android:layout_width="match_parent"
41         android:layout_height="wrap_content"
42         android:layout_marginTop="10dp"
43         android:text="长按点击事件"/>
44 
45 </LinearLayout>

 

JAVA 代码:

 1 package com.example.a11658.buttondemo;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.Button;
 7 import android.widget.Toast;
 8 
 9 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
10     Button button2, button3, button4, button5;
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16 
17         initView();
18     }
19 
20     private void initView() {
21         button2 = findViewById(R.id.button2);
22         button3 = findViewById(R.id.button3);
23         button4 = findViewById(R.id.button4);
24         button5 = findViewById(R.id.button5);
25 
26         button2.setOnClickListener(new View.OnClickListener() {
27             @Override
28             public void onClick(View v) {
29                 //匿名内部类实现点击事件
30                 Toast.makeText(MainActivity.this, "匿名内部类的点击事件", Toast.LENGTH_SHORT).show();
31             }
32         });
33 
34         button3.setOnClickListener(new MyListener());
35         button4.setOnClickListener(this);
36         button5.setOnLongClickListener(new View.OnLongClickListener() {
37             @Override
38             public boolean onLongClick(View v) {
39                 Toast.makeText(MainActivity.this, "长按事件", Toast.LENGTH_SHORT).show();
40                 return false;
41             }
42         });
43     }
44 
45     public void doClick(View view) {
46         //xml通过onClick实现点击事件
47         Toast.makeText(this, "xml点击实现", Toast.LENGTH_SHORT).show();
48     }
49 
50     @Override
51     public void onClick(View v) {
52         //继承方式
53         Toast.makeText(this, "继承点击", Toast.LENGTH_SHORT).show();
54     }
55 
56     class MyListener implements View.OnClickListener {
57 
58         @Override
59         public void onClick(View v) {
60             //自定义方式
61             Toast.makeText(MainActivity.this, "自定义点击事件", Toast.LENGTH_SHORT).show();
62         }
63     }
64 }
备注:Button数量不多的情况下推荐使用第二种,匿名内部类的方式实现;反之则推荐使用第四种,Activity继承View.OnClickListener实现

代码链接:https://pan.baidu.com/s/1hlkrcybgMUrS2rrslnDwGg

android listview 点击事件失效

android listview 点击事件失效

在 item 根目录写

android:descendantFocusability="blocksDescendants"

Android ScrollView 滑动事件和子控件点击事件冲突

Android ScrollView 滑动事件和子控件点击事件冲突

问题描述:

父控件是一个 ScrollView,然后上面排满了按钮,类似于支付宝

这样,按钮都有 onclick 事件,这样滑动的时候如果接触点在按钮上,ScrollView 就滑不动了。

解决方案是重写 ScrollView 的 onInterceptTouchEvent 方法:

public class PersonScrollView extends ScrollView{

    public PersonScrollView(Context context) {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_MOVE) {
                return true;
        }
        return false;
    }
}

原理很简单,就是事件在响应链里面是一直传递到叶子控件才进行处理,而在我的这个场景里,触点在按钮上的时候事件的叶子节点就是按钮,所以 ScrollView 不会响应滑动事件,解决思路就是在滑动事件传递到 ScrollView 的时候就进行拦截响应,不再往下传递。

关于android 模拟用户点击事件安卓模拟用户点击的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Android adb 模拟滑动 按键 点击事件、Android Button 四种点击事件和长按事件、android listview 点击事件失效、Android ScrollView 滑动事件和子控件点击事件冲突等相关知识的信息别忘了在本站进行查找喔。

本文标签: