1,key -- 实体按键,
现在手机物理按键越来越少
常见的有 KEYCODE_VOLUME_DOWN/UP KEYCODE_POWER KEYCODE_BACK KEYCODE_HOME KEYCODE_MENU
在一个activity 重载父类 的下面这三个方法来处理按键事件
public boolean onKeyDown(int keyCode, KeyEvent event)
public boolean onKeyUp(int keyCode, KeyEvent event)
public boolean onKeyLongPress(int keyCode, KeyEvent event)
很明显,长按事件一般是放到 onKeyLongPress 函数中来处理的。
那具体是怎么做的呢,下面请看步骤:
第一步,先在 onKeyDown 函数中判断 event.getRepeatCount 的次数(实际上长按就是由一系列的onKeyDown事件触发的)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_UP:
if (mCameraAppView.getVisibility() != View.VISIBLE) {
return false;
}
Log.v(TAG,"onKeyDown event.getRepeatCount() "+event.getRepeatCount());
if (event.getRepeatCount() == 0) {
event.startTracking();
return true;
}
return true;
}
return super.onKeyDown(keyCode, event);
}
如果一直按着不放,通过打 log 可以看到
onKeyDown event.getRepeatCount() 0
onKeyDown event.getRepeatCount() 1
onKeyDown event.getRepeatCount() 2
onKeyDown event.getRepeatCount() 3
onKeyDown event.getRepeatCount() 4
onKeyDown event.getRepeatCount() 5
onKeyDown event.getRepeatCount() 6
onKeyDown event.getRepeatCount() 7
onKeyDown event.getRepeatCount() 8
onKeyDown event.getRepeatCount() 9
onKeyDown event.getRepeatCount() 10
onKeyDown event.getRepeatCount() 11
onKeyDown event.getRepeatCount() 12
……
第二步,重载 onKeyLongPressed 函数,在这个函数中你可以添加你的处理
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
Log.v(TAG,"key long pressed keyCode = "+keyCode);
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
Log.v(TAG,"KEYCODE_VOLUME_UP/DOWN long pressed");
lockLongPressKey = true;
onShutterButtonLongPressed();
return true;
default:
break;
}
return super.onKeyLongPress(keyCode, event);
}
第三步,如果在onKeyUp 函数中也处理了相同的按键的话,那么需要对 onKeyLongPress 和 onKeyUp 中该按键的处理作互斥处理了
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
Log.v(TAG, "onKeyUp event.getRepeatCount() "+event.getRepeatCount());
if(lockLongPressKey){
lockLongPressKey = false;
return true;
}
if (event.getRepeatCount() == 0) {
Log.v(TAG, "prepare for onShutterButtonClick..");
if (mEnableRecordBtn && mReviewImage.getVisibility() != View.VISIBLE
&& mBgLearningMessageFrame.getVisibility() == View.GONE) {
if (mShutterButton != null && mShutterButton.isEnabled()) {
onShutterButtonFocus(false);
collapseCameraControls();//fulin@20121101@fix PD1218 B121027-224
onShutterButtonClick();
}
}
}
if (mCameraAppView.getVisibility() != View.VISIBLE) {
return false;
}
return true;
}
return super.onKeyUp(keyCode, event);
}
互斥处理的原因是,长按事件处理完后,松开按键后就会执行 onKeyUp 函数。而这两处针对相同的按键想要实现的功能又不一样
固在此添加一个 private boolean lockLongPressKey = false; 变量,在 onKeyLongPress 函数中处理过以后,在 onKeyUp中便不再处理。
2, button
自定义的 button 长按事件则是通过实现 OnLongClickListener 接口来做到的。
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MyActivity extends Activity implements OnLongClickListener{
Button button;//声明按钮的引用
public void onCreate(Bundle savedInstanceState) {//重写的onCreate方法
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) this.findViewById(R.id.button);//得到按钮的引用
button.setTextSize(20);
button.setOnLongClickListener(this);//注册监听
}
public boolean onLongClick(View v) {//实现接口中的方法
if(v == button){//当按下的是按钮时
Toast.makeText(
this,
"长时间按下了按钮",
Toast.LENGTH_SHORT
).show();//显示提示
}
return false;
}
}