關鍵詞:MotionEvent,模擬按鍵,模擬點擊事件,主動彈出輸入法,彈出軟鍵盤。 歡迎轉載並說明轉自:http://go.rritw.com/blog.csdn.net/aminfo/articl
關鍵詞:MotionEvent,模擬按鍵,模擬點擊事件,主動彈出輸入法,彈出軟鍵盤。 歡迎轉載並說明轉自:http://go.rritw.com/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>
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://go.rritw.com/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; } } } }
From:CSDN |
全站熱搜
留言列表