关闭

vernice的专栏

136
原创
18
转载
1
译文
79
评论
378363
访问

2017七月
18

Simulate a key press on Android

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));


img

vernice

等级:

排名:第5994名

博客专栏
文章分类
文章存档
阅读排行
评论排行
推荐文章
最新评论
img