如果我点击一个文本输入,我希望能够点击其他地方,以便再次取消键盘(不是返回键)。在我读过的所有教程和博客文章中,我没有发现一丁点关于这方面的信息。

这个基本的例子在模拟器中的react-native 0.4.2中仍然不能为我工作。还不能在我的iPhone上试试。

<View style={styles.container}>
  <Text style={styles.welcome}>
    Welcome to React Native!
  </Text>
  <Text style={styles.instructions}>
    To get started, edit index.ios.js
  </Text>
  <Text style={styles.instructions}>
    Press Cmd+R to reload,{'\n'}
    Cmd+D or shake for dev menu
  </Text>
  <TextInput
    style={{height: 40, borderColor: 'gray', borderWidth: 1}}
    onEndEditing={this.clearFocus}
  />
</View>

当前回答

更新了React Native 0.39中ScrollView的用法

<ScrollView scrollEnabled={false} contentContainerStyle={{flex: 1}} />

虽然,两个TextInput框仍然有一个问题。如。用户名和密码表单现在在切换输入时将忽略键盘。很想得到一些建议,以保持键盘在使用ScrollView切换TextInputs之间。

其他回答

使用这个包react-native-keyboard-aware-scroll-view

使用该组件作为根组件

因为这个包react-native-keyboard-aware-scroll-view也有一个scrollView,你需要添加这个:

< KeyboardAwareScrollView keyboardShouldPersistTaps =“不同”> < ScrollView keyboardShouldPersistTaps = "不同" > < / ScrollView > < / KeyboardAwareScrollView >

我刚刚使用最新的React Native版本(0.4.2)进行了测试,当你点击其他地方时,键盘就会消失。

供你参考:你可以设置一个回调函数,当你解散键盘时,通过将它分配给“onenditing”道具来执行。

有几种方法, 如果你像onPress一样控制事件,你可以使用:

import { Keyboard } from 'react-native'

onClickFunction = () => {
     Keyboard.dismiss()
}

如果你想关闭键盘时使用滚动:

<ScrollView keyboardDismissMode={'on-drag'}>
     //content
</ScrollView>

更多的选项是当用户点击键盘外:

<KeyboardAvoidingView behavior='padding' style={{ flex: 1}}>
    //inputs and other content
</KeyboardAvoidingView>

使用ScrollView代替带有keyboardShouldPersistTaps={"always"}属性的View。当我们点击屏幕内的任何地方时,它会隐藏键盘

<ScrollView keyboardShouldPersistTaps={"always"}>
 <View style={styles.container}>
  <Text style={styles.welcome}>
    Welcome to React Native!
  </Text>
  <Text style={styles.instructions}>
    To get started, edit index.ios.js
  </Text>
  <Text style={styles.instructions}>
    Press Cmd+R to reload,{'\n'}
    Cmd+D or shake for dev menu
  </Text>
  <TextInput
    style={{height: 40, borderColor: 'gray', borderWidth: 1}}
    onEndEditing={this.clearFocus}
  />
 </View>
</ScrollView>

将此用于自定义撤销

var dismissKeyboard = require('dismissKeyboard');

var TestView = React.createClass({
    render: function(){
        return (
            <TouchableWithoutFeedback 
                onPress={dismissKeyboard}>
                <View />
            </TouchableWithoutFeedback>
        )
    }
})