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事件时,处理流程为:

 android keydown touch事件传递_第1张图片

再回过头来看onKeyDown事件,其实也和touch事件的处理流程一样。

Activity获取到keydown事件 ------>分发给子View MyButton ------> 调用子View 的监听器方法------> 调用子 View 的onKeyDown方法 ------> 调用Activity的onKeyDown方法

你可能感兴趣的:(keydown,android,event,touch)