开发者

onKeyDown not working

I am writing code to change a TextView when the volume up/down key gets pressed to update the TextView of the system volume % set.I now have this code which works once but it Overrides the functionality of up/down, is there a way to re-write it or exclude it from being overid?

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {


            //system volume
            int curVolume = 开发者_运维技巧audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            int i = curVolume;  
            String aString = Integer.toString(i);   

            TextView sysVol = (TextView)findViewById(R.id.systemVolume);
            sysVol.setText(aString);
            sysVol.invalidate();

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}


Don't return true in your code, but use

return super.onKeyDown(keyCode, event);

instead like in the other branches. Returning true means the event got handled properly and no further action will be taken. Returning false means the event has still to be handled.


Onkeydown doesn't propagate to the textview. Attach it to your activity instead and target the textview from that

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜