Khi tôi sắp sửa gửi của tôi câu trả lời tôi phát hiện ra một số đã có một số loại giải pháp ....
Nhưng đây là của tôi, đơn giản và hoạt động như một sự quyến rũ. Chỉ cần một lá cờ;)
Mã này phát hiện các phím tắt và long, khi thời gian dài xảy ra không có shortpress nào được kích hoạt!
Lưu ý: nếu bạn muốn khối lượng bình thường và hành vi xuống thay đổi sự trở lại đúng trong phương pháp OnKeyPress cuộc gọi siêu như thế này:
event.startTracking();
if(event.getRepeatCount() == 0){
shortPress = true;
}
//return true;
return super.onKeyDown(keyCode, event);
Mã mà không gọi siêu:
private boolean shortPress = false;
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
shortPress = false;
Toast.makeText(this, "longPress", Toast.LENGTH_LONG).show();
return true;
}
//Just return false because the super call does always the same (returning false)
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
event.startTracking();
if(event.getRepeatCount() == 0){
shortPress = true;
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
if(shortPress){
Toast.makeText(this, "shortPress", Toast.LENGTH_LONG).show();
} else {
//Don't handle longpress here, because the user will have to get his finger back up first
}
shortPress = false;
return true;
}
return super.onKeyUp(keyCode, event);
}
Mã dưới đây là với phím tăng âm lượng, chỉ cần chọn mặt của bạn;)
private boolean shortPress = false;
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
shortPress = false;
Toast.makeText(this, "longPress Volume Down", Toast.LENGTH_LONG).show();
return true;
} else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
shortPress = false;
Toast.makeText(this, "longPress Volume Up", Toast.LENGTH_LONG).show();
return true;
}
//Just return false because the super call does always the same (returning false)
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
event.startTracking();
if(event.getRepeatCount() == 0){
shortPress = true;
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
if(shortPress){
Toast.makeText(this, "shortPress Volume Down", Toast.LENGTH_LONG).show();
} else {
//Don't handle longpress here, because the user will have to get his finger back up first
}
shortPress = false;
return true;
} else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
if(shortPress){
Toast.makeText(this, "shortPress Volume up", Toast.LENGTH_LONG).show();
} else {
//Don't handle longpress here, because the user will have to get his finger back up first
}
shortPress = false;
return true;
}
return super.onKeyUp(keyCode, event);
}
sau đó thử với khóa (KeyEvent.KEYCODE_VOLUME_UP) thay vì khóa phím; nếu bấm lâu sẽ gọi trước khi bấm phím, nếu bấm lâu gọi là dừng hành động trong khóa bằng cách sử dụng cờ – Sandy09
Đó là phím tăng âm lượng, không phải chỉ báo cho biết khóa được giữ hoặc thứ gì đó tương tự. –
Tôi đang thử mọi thứ ở thời điểm này –