Android 模拟MotionEvent事件 触发输入法
Android 模拟MotionEvent事件 触发输入法
关键词: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;
- }
- }
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。