为什么没有这项工作?我想创建一个onClickListener的产生同样的效果pressing在D盘的下键按钮。 Eclipse的给我一个错误,他说:不能让一个静态引用从类型InputMethodService的非静态方法sendDownUpKeyEvents(INT)帮助
! downButton.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图查看){
InputMethodService.sendDownUpKeyEvents(0x00000014);
}
解决方案
您正试图在一个静态的方式调用非静态方法。你需要先获得服务的实例,然后调用实例上的方法。此外,你正在做的主要preSS模拟的方式看起来不正确。 UPD: 一些挖后,我已经成功地模拟按键事件,请尝试:
新主题(新的Runnable(){
@覆盖
公共无效的run(){
新规范()sendKeyDownUpSync(KeyEvent.KEY code_DPAD_DOWN);
}
})。开始();
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);
}
解决方案
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:
new Thread(new Runnable() {
@Override
public void run() {
new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
}
}).start();
上一篇:触发系统音量栏音量、系统
下一篇:Android的ListView的适配器notifyDataSetInvalidated()与notifyDataSetChanged()适配器、ListView、Android、notifyData