Simulate a key press on Android

来源:互联网 发布:数据迁移的重要性 编辑:程序博客网 时间:2024/06/02 03:28

Method 1:

import android.app.Instrumentation;new Thread(new Runnable() {    @Override    public void run() {        try {            Instrumentation inst = new Instrumentation();            for ( int i = 0; i < 10; ++i ) {                inst.sendKeyDownUpSync(KeyEvent.KEYCODE_2);                Thread.sleep(2000);                inst.sendKeyDownUpSync(KeyEvent.KEYCODE_3);                Thread.sleep(2000);            }        }        catch(InterruptedException e){        }    }}).start();



Method 2:

import android.view.inputmethod.BaseInputConnection;import android.view.KeyEvent;BaseInputConnection inputConnection = new BaseInputConnection(edittext1, true);inputConnection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_1));


阅读全文
1 0
原创粉丝点击