美文网首页
React Native Android实现键盘focus的na

React Native Android实现键盘focus的na

作者: xinguoshui | 来源:发表于2018-12-06 13:39 被阅读0次

    Android原生可以通过设置android:nextFocusForward等指定导航顺序。RN没有内置支持,可以通过发送event的方式进行指定。

    <View  ref={(ref: any) => (this.rootView = ref)}>
    ...
    </View>
    

    然后在componentDidMount的时候发送event给native。

        componentDidMount() {
            if (context.isAndroid() && this.rootView !== 'undefined') {
                var focusOrderList = [];
                ... // Generate focus order list here
                var UIManager = require('UIManager');
                var rootViewHandle = findNodeHandle(this.rootView);
                UIManager.dispatchViewManagerCommand(
                    rootViewHandle,
                    ViewCommands.NEXT_FOCUS_COMMAND,
                    focusOrderList,
                );
            }
        }
    
    

    自己实现一个ViewManager,继承自ReactViewManager,把默认的ReactViewManager替换掉,然后实现receiveCommand方法。

        @Override
        public void receiveCommand(final ReactViewGroup root, int commandId, @Nullable ReadableArray args) {
            super.receiveCommand(root, commandId, args);
    
             if (commandId == CMD_NEXT_FOCUS) {
                if (args != null) {
                    int length = args.size();
                    for (int i = 0; i < length - 1;i++) {
                        View firstView = root.getRootView().findViewWithTag(args.getString(i));
                        View nextFocusView = root.getRootView().findViewWithTag(args.getString(i+1));
                        if (firstView != null && nextFocusView != null) {
                            firstView.setNextFocusForwardId(nextFocusView.getId());
                        }
                    }
                }
            }
        }
    
    

    相关文章

      网友评论

          本文标题:React Native Android实现键盘focus的na

          本文链接:https://www.haomeiwen.com/subject/zpxocqtx.html