继承View类后onKeyUp无法触发

来源:互联网 发布:python go 编辑:程序博客网 时间:2024/05/17 01:52

如果直接继承View类,然后override掉onKeyUp或者其它按键事件,会发现按键后并没有触发这个View派生类的onKeyUp事件,触发的是Activity的事件。“You will get key events only if your view is focusable and is focused. In the case of a game, you can set your view to be focusable in touch mode to get the key events pretty much automatically.” 验证了一下,确实isFocusable是false的。调用一下setFocusable(true)或者setFocusableInTouchMode(true)就可以捕获按键事件了。