android keydown touch事件传递
1、 onKeyDown事件
Android系统最先触发按键上绑定的事件监听器,然后才触发该组件提供的事件回调方法,最后传播到该组件所在的Activity
public class MyButton extends Button {
publicMyButton(Context context, AttributeSet set) {
super(context,set);
}
@Override
public booleanonKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode,event);
Log.v(“-MyButton-”,“the onKeyDown in MyButton”);
returnfalse;
}
}
public class Propagation extents Activity {
@Override
public voidonCreate(Bundle savedInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.main);
Buttonbn = (Button) findViewById(R.id.bn);
bn.setOnKeyListener(newOnKeyListener() {
@Override
publicBoolean onKey(View source, int keyCode, KeyEvent event) {
if(event.getAction()== KeyEvent.ACTION_DOWN) {
Log.v(“-Listener-”,“the onKeyDown in Listener”);
}
returnfalse;
}
});
}
@Override
public booleanonKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode,event);
Log.v(“-Activity-”,“the onKeyDown in Activity”);
returnfalse;
}
}
执行后,logcat输出为:
-Listener- the onKeyDown in Listener
-MyButton- the onKeyDown in MyButton
-Activity- the onKeyDown in Activity
2、Touch 事件传递
android系统中的每个View的子类都具有下面三个和TouchEvent处理密切相关的方法:
1)public booleandispatchTouchEvent(MotionEventev) 这个方法用来分发TouchEvent
2)public booleanonInterceptTouchEvent(MotionEvent ev)这个方法用来拦截TouchEvent
3)public booleanonTouchEvent(MotionEventev)这个方法用来处理TouchEvent
另外,还有的View会添加OnTouch的事件监听器。
当有ACTION_DOWN事件时,处理流程为:
再回过头来看onKeyDown事件,其实也和touch事件的处理流程一样。
Activity获取到keydown事件 ------>分发给子View MyButton ------> 调用子View 的监听器方法------> 调用子 View 的onKeyDown方法 ------> 调用Activity的onKeyDown方法
更多相关文章
- 获取Android系统中全部正在运行的程序
- Android(安卓)Fragment详解(二):Fragment创建及其生命周期
- android调用webservice方法,参数或返回值使用服务器端类
- Android(安卓)中使用自定义字体的方法
- 每次吃一点Androidの小知识
- Android(安卓)实现事件监听器的五种处理方法
- android 初识EventBus
- Android(安卓)解决getColor()或getDrawable()方法过时的办法
- Android获取手机通话记录的方法