Android:帮助创建一个按钮,其产生与按方向键上的向下键相同的结果? (第 2 部分)
为什么这不起作用?我正在尝试为按钮创建一个 onClickListener,该按钮产生与按方向键上的“向下”键相同的效果。 Eclipse 给我一个错误,说:“无法从类型 InputMethodService 中对非静态方法 sendDownUpKeyEvents(int) 进行静态引用” 救命!
downButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
InputMethodService.sendDownUpKeyEvents(0x00000014);
}
Why doesn't this work?? I am trying to create an onClickListener for a button that produces the same effect as pressing the "down" key on the D-pad. Eclipse gives me an error, saying: "Cannot make a static reference to the non-static method sendDownUpKeyEvents(int) from the type InputMethodService" Help!
downButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
InputMethodService.sendDownUpKeyEvents(0x00000014);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您试图以静态方式调用非静态方法。您需要先获取服务的实例,然后调用该实例上的方法。此外,您模拟按键的方式看起来也不正确。
更新:
经过一番挖掘后,我成功模拟了关键事件,请尝试:
You're trying to invoke non-static method in a static way. You need to obtain an instance of the service first and then invoke the method on the instance. Also the way you're doing the simulation of keypress looks incorrect.
UPD:
After some digging I've managed to simulate key event, try:
相同的解决方案,只是需要一个参数。
只需像这样调用并传递
KeyEvent.KEYCODE
InjectKeys(KeyEvent.KEYCODE_DPAD_DOWN);
我的答案是
Same solution, just will take a parameter.
Just call and pass the
KeyEvent.KEYCODE
like soInjectKeys(KeyEvent.KEYCODE_DPAD_DOWN);
My answer is based on this other answer, I just modified it to use parameters.