Android 模拟触摸动作MotionEvent事件

模拟触屏事件需要两个步骤,一是获得事件对象MotionEvent,二是分发该事件对象。

1.获得事件对象MotionEvent

获得一个MotionEvent对象,这里我们使用MotionEvent的obtain()方法,参数如下

MotionEvent.obtain(downTime, eventTime, action, x, y, metaState);

举个例子:
onTouch(mPageView, MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, screenWidth / 2, screenHeight / 2, 0));
就表示在屏幕中间模拟一次ACTION_DOWN事件

2.分发该事件对象

public boolean dispatchTouchEvent(MotionEvent ev)  这个方法用来分发MotionEvent

下面这个示例程序的流程是点击一个button然后触发一个EditText的获取焦点事件

一、布局文件showime.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical">
               
    <Button android:id="@+id/ButtonIME"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5px"
        android:text="弹出软键盘输入法" />
       
           
    <EditText android:id="@+id/EditTextIME"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5px"
        android:text="显示输入法键盘"/>
   
</LinearLayout>

二、Activity,ShowIME.java文件源码:

package org.shuxiang.test;
   
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
   
public class ShowIME  extends Activity
{
    private EditText et;
    private Button bt;
    private static Handler messageHandler;
       
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.showime);
           
        bt = (Button) findViewById(R.id.ButtonIME);
        bt.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, et.getLeft()+5, et.getTop()+5, 0));
                et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, et.getLeft()+5, et.getTop()+5, 0));
            }
        });        
        et = (EditText) findViewById(R.id.EditTextIME);
           
           
        Looper looper = Looper.myLooper();
        messageHandler = new MessageHandler(looper);        
        //此处的作用是延迟1秒,然后激活点击事件
        //欢迎转载并说明转自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0111/799.html
        new Thread()
        {
            @Override
            public void run()
            {
                synchronized(this)
                {
                    try
                    {
                        wait(1000); //1秒
                    }
                    catch (InterruptedException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                Message message = Message.obtain();
                message.what = 1;
                messageHandler.sendMessage(message);
            }
        }.start();        
    }
       
    class MessageHandler extends Handler
    {
        public MessageHandler(Looper looper)
        {
            super(looper);
        }
        @Override
        public void handleMessage(Message msg)
        {
            Bundle bundle = msg.getData();
            switch (msg.what)
            {
            case 1:
                //模拟点击按钮
                bt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, et.getLeft()+5, et.getTop()+5, 0));
                bt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, et.getLeft()+5, et.getTop()+5, 0));
                   
                //以下代码模拟点击文本编辑框
                //et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, et.getLeft()+5, et.getTop()+5, 0));
                //et.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, et.getLeft()+5, et.getTop()+5, 0));
                break;
            default:
                break;
            }
               
        }
    }
}