• KeyBord事件从Activtiy层往下分发详细过程代码示例


    step1:调用Activity成员函数dispatchKeyEvent

    public boolean dispatchKeyEvent(KeyEvent event) {

        // Let action bars open menus in response to the menu key prioritized over
    // the window handling it
    if (event.getKeyCode() == KeyEvent.KEYCODE_MENU &&
    mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
    return true;
    }
    Window win = getWindow();
    if (win.superDispatchKeyEvent(event)) {
    return true;
    }
    View decor = mDecor;
    if (decor == null) decor = win.getDecorView();
    return event.dispatch(this, decor != null
    ? decor.getKeyDispatcherState() : null, this);
    }

    step2:Activity成员函数dispatchKeyEvent中调用phoneWindow的成员函数superDispatchKeyEvent
        Window win = getWindow();
    if (win.superDispatchKeyEvent(event)) {
    return true;
    }

    setp3:在PhoneWindow的
    superDispatchKeyEvent函数中调用DecorView的成员函数uperDispatchKeyEvent
        public boolean superDispatchKeyEvent(KeyEvent event) {
            return mDecor.superDispatchKeyEvent(event);
        }

    step4:ViewGroup的成员函数DispatchKeyEvent
    private final class DecorView extends FrameLayout implements RootViewSurfaceTaker {
    ...    
    public DecorView(Context context, int featureId) {
    super(context);
    mFeatureId = featureId;
    }
    @Override
    public boolean superDispatchKeyEvent(KeyEvent event) {
               if (super.dispatchKeyEvent(event)) {
                   return true;
               }
    ...
    }

    step5:通过mFocused的包含关系实现隧道式分发
    public boolean dispatchKeyEvent(KeyEvent event) {
    if (mInputEventConsistencyVerifier != null) {
    mInputEventConsistencyVerifier.onKeyEvent(event, 1);
    }

    if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
    == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
    if (super.dispatchKeyEvent(event)) {
    return true;
    }
    } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
    == PFLAG_HAS_BOUNDS) {
    if (mFocused.dispatchKeyEvent(event)) {
    return true;
    }
    }

    if (mInputEventConsistencyVerifier != null) {
    mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
    }
    return false;
    }
     
     








  • 相关阅读:
    Android 获取自带浏览器上网记录
    android 中的 ViewPager+ Fragment
    Git分支操作
    图形验证码的识别
    mac平台搭建安卓开发环境
    [报错集合]Uncaught ReferenceError: xxx is not defined
    Centos安装Python3
    VSCode 代码格式化 快捷键
    Mac下用Parallels Desktop安装Ubuntu
    请求头headers
  • 原文地址:https://www.cnblogs.com/lianghe01/p/5712791.html
Copyright © 2020-2023  润新知