【发布时间】:2017-10-17 08:22:02
【问题描述】:
我想模拟一系列关键事件。经过一段时间的搜索,我找到了BaseInputConnection 类型的对象的sendkeyEvent 方法。所以在MainActivity 类的onCreate() 函数中,我向一个textview 发送了3 次keyevent,我将它绑定到这个activity。
@Override
protected void onCreate(Bundle savedInstanceState){
...
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(stringFromJNI);
tv.setOnKeyListener(this);
BaseInputConnection mInputConnection = new BaseInputConnection(tv, true);
//BaseInputConnection mInputConnection = new BaseInputConnection(tv, false);
KeyEvent kleft = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT);
for(int i=0;i<3;i++){
mInputConnection.sendKeyEvent(kleft);
Log.d(TAG, "keyevent sent");
}
}
MainActivity 类实现了接口OnKeyListener。
@Override
public boolean OnKey(View v, int keyCode, KeyEvent){
Log.d(TAG, "OnKey starts.");
if(event.getKeyCode()==KeyEvent.KEYCODE_DPAD_LEFT && event.getAction()==KeyEvent.ACTION_DOWN){
Log.d(TAG, "key left.");
return true;
}
return false;
}
查看logcat的输出后,发现“keyevent sent”是printend 3次,意思是keyevents被发送了。但是没有“OnKey 启动”。为什么没有调用 OnKey() 函数?
【问题讨论】:
标签: android android-activity android-view key-events