【问题标题】:Focus Picker component on react native焦点选择器组件反应原生
【发布时间】:2019-06-14 02:31:19
【问题描述】:

我有一个 Picker 组件作为 react native 表单的一部分。
我想在用户单击键盘上的提交按钮时聚焦选择器,就像我可以使用组件的道具聚焦其他文本输入一样:

onSubmitEditing={() => {this.nextTextInput.focus()}}
ref={ref => {this.thisTextInput = ref}}

这可能吗?
我应该使用什么选择器方法?
如果选择器在焦点事件上打开弹窗就可以了。

提前致谢!

【问题讨论】:

    标签: javascript reactjs react-native textinput picker


    【解决方案1】:

    以同样的方式,您可以像这样聚焦您的组件;

    constructor(props) {
        super(props);
        this.picker = React.createRef();
        this.focusPicker = this.focusPicker.bind(this);
    }
    
    focusPicker() {
        this.picker.current.focus();
    }
    
    render() {
        return (
            <View>
                <Picker ref={this.picker}>
                ........
                </Picker>
                <TouchableOpacity onPress={this.focusPicker}/>
            </View>
        );
    }
    

    【讨论】:

    • 我尝试了您代码的所有变体,但仍然收到“_this.picker.current.focus 不是函数”错误。它说 this.picker.current.focus 是未定义的。我也试过没有“电流”。也许我做错了什么。我将修改我的代码并用更多细节更新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    相关资源
    最近更新 更多