如何在提交后获得React Native TextInput以保持焦点?

问题描述:

我可以解释我正在尝试做什么,但this ReactJS example是我想要的一个演练。问题是我无法弄清楚equivelant会是什么原因。如何在提交后获得React Native TextInput以保持焦点?

基本上,当我按下TextInput中的return时,我希望清除文本并保持焦点。

有什么想法?

我已经提交了一个PR blurOnSubmit属性。

将它设置为falseTextInput从不模糊,但onSubmitEditing仍然引发。

希望它合并。 :)

https://github.com/facebook/react-native/pull/2149

+2

耶!它合并了! – 2015-11-05 20:50:42

我想出了以下(工作)解决方案:

var NameInput = React.createClass({ 
    getInitialState() { 
    return { 
     textValue: '' 
    } 
    }, 

    clearAndRetainFocus: function(evt, elem) { 
    this.setState({textValue: elem.text}); 
    setTimeout(function() { 
     this.setState({textValue: this.getInitialState().textValue}); 
     this.refs.Name.focus(); 
    }.bind(this), 0); 
    }, 

    render() { 
    return(
     <TextInput 
     ref='Name' 
     value={this.state.textValue} 
     onEndEditing={this.clearAndRetainFocus} /> 
    ) 
    } 
}); 

所以,基本上,当我们最终的编辑,我们将textValue状态设置为以后的TextInput和右边的值(setTimeout)我们将它切换回默认值(空)并保留元素的焦点。

+0

这只能工作一半的时间。增加超时有帮助,但必须有一个始终有效的解决方案。 – brysgo 2015-04-04 01:47:10

+0

使用软件键盘更可靠,但键盘跳转关闭然后打开。 – brysgo 2015-04-04 02:48:38

+1

在onSubmitEditing上做什么? – 2015-07-05 03:20:38