Android控件TextView的实现原理分析(8)

发表于:2013-04-02来源:Csdn作者:罗升阳点击数: 标签:Android
Step 8. PhoneWindow.superDispatchKeyEvent [java] view plaincopyprint? public class PhoneWindow extends Window implements MenuBuilder.Callback { ...... // This is the top-level view of the window, cont

  Step 8. PhoneWindow.superDispatchKeyEvent

  [java] view plaincopyprint?

  public class PhoneWindow extends Window implements MenuBuilder.Callback {

  ......

  // This is the top-level view of the window, containing the window decor.

  private DecorView mDecor;

  ......

  @Override

  public boolean superDispatchKeyEvent(KeyEvent event) {

  return mDecor.superDispatchKeyEvent(event);

  }

  ......

  }

  public class PhoneWindow extends Window implements MenuBuilder.Callback {

  ......

  // This is the top-level view of the window, containing the window decor.

  private DecorView mDecor;

  ......

  @Override

  public boolean superDispatchKeyEvent(KeyEvent event) {

  return mDecor.superDispatchKeyEvent(event);

  }

  ......

  }

  这个函数定义在文件frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindow.java中。

  PhoneWindow类的成员变量mDecor描述的是当前正在处理的Activity组件窗口的顶层视图,PhoneWindow类的成员函数superDispatchKeyEvent通过调用它所指向的一个DecorView对象的成员函数superDispatchKeyEvent来处理参数event所描述的键盘事件。

  Step 9. DecorView.superDispatchKeyEvent

  [java] view plaincopyprint?

  public class PhoneWindow extends Window implements MenuBuilder.Callback {

  ......

  private final class DecorView extends FrameLayout implements RootViewSurfaceTaker {

  ......

  public boolean superDispatchKeyEvent(KeyEvent event) {

  return super.dispatchKeyEvent(event);

  }

  ......

  }

  ......

  }

  public class PhoneWindow extends Window implements MenuBuilder.Callback {

  ......

  private final class DecorView extends FrameLayout implements RootViewSurfaceTaker {

  ......

  public boolean superDispatchKeyEvent(KeyEvent event) {

  return super.dispatchKeyEvent(event);

  }

  ......

  }

  ......

  }

  这个函数定义在文件frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindow.java中。

  DecorView类的成员函数superDispatchKeyEvent的实现很简单,它只是调用父类ViewGroup的成员函数dispatchKeyEvent来处理参数event所描述的键盘事件。

  Step 10. ViewGroup.dispatchKeyEvent

  [java] view plaincopyprint?

  public abstract class ViewGroup extends View implements ViewParent, ViewManager {

  ......

  @Override

  public boolean dispatchKeyEvent(KeyEvent event) {

  if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {

  return super.dispatchKeyEvent(event);

  } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {

  return mFocused.dispatchKeyEvent(event);

  }

  return false;

  }

  ......

  }

  public abstract class ViewGroup extends View implements ViewParent, ViewManager {

  ......

  @Override

  public boolean dispatchKeyEvent(KeyEvent event) {

  if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {

  return super.dispatchKeyEvent(event);

  } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {

  return mFocused.dispatchKeyEvent(event);

  }

  return false;

  }

  ......

  }

  这个函数定义在文件frameworks/base/core/java/android/view/ViewGroup.java中。

  ViewGroup类的成员函数dispatchKeyEvent的实现与在前面的Step 3中所介绍的ViewGroup类的成员函数dispatchKeyEventPreIme的实现是类似的,即如果当前正在处理的视图容器能够获得焦点并且该视图容器的大小已经计算好了,那么就会将参数event所描述的键盘事件分发给它的父类View的成员函数dispatchKeyEvent来处理,否则的话,如果当前正在处理的视图容器有一个焦点子视图,并且这个焦点子视图的大小已经计算好了,那么就将参数event所描述的键盘事件分发给该焦点子视图的父类View的成员函数dispatchKeyEvent来处理。

  从前面的调用过程可以知道,当前正在处理的视图容器即为Activity组件窗口的顶层视图。我们假设在该顶层视图中,获得焦点的是一个TextView控件,并且这个TextView控件的大小已经计算好了,那么接下来就会调用这个TextView控件的父类View的成员函数dispatchKeyEvent来处理参数event所描述的键盘事件。

  Step 11. View.dispatchKeyEvent

  [java] view plaincopyprint?

  public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {

  ......

  private OnKeyListener mOnKeyListener;

原文转自:http://blog.csdn.net/luoshengyang/article/details/8636153