问题
Is there a way to hide the volume ui when the volume_up/volume_down key is pressed. I understand that it can be done with an activity when it doesnt seem to work when tiggered when a dialog is shown.
Is there an solution?
回答1:
This should work with the Dialog class:
AudioManager man = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
dialog.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_VOLUME_UP:
man.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
man.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
return true;
default:
return super.onKeyDown(keyCode, event);
}
}
});
Like at Android: Hide Volume change bar from device?
回答2:
Try this code, it should disable the toast message of the Volume
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
//Do something to hide the view
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
manager.adjustVolume(AudioManager.ADJUST_LOWER, 0);
}
if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)){
//Do something to hide the view
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
manager.adjustVolume(AudioManager.ADJUST_RAISE, 0);
}
return true;
}
来源:https://stackoverflow.com/questions/18335026/hide-ui-when-volume-up-down-pressed-in-a-dialog