Android 模拟MotionEvent事件 触发输入法

25 篇文章 0 订阅
本文介绍如何在Android中模拟MotionEvent事件,以主动弹出输入法和软键盘。通过布局文件showime.xml和Activity ShowIME.java的代码实现,详细讲解了模拟按键和点击事件的操作过程。
摘要由CSDN通过智能技术生成

关键词:MotionEvent,模拟按键,模拟点击事件,主动弹出输入法,弹出软键盘。

欢迎转载并说明转自:http://blog.csdn.net/aminfo/article/details/7887964

 

一、布局文件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://blog.csdn.net/aminfo/article/details/7887964
        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;
        	}
        	
        }
    }
}


欢迎转载并说明转自:http://blog.csdn.net/aminfo/article/details/7887964

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值