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

这个基本的例子在模拟器中的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导入键盘:

import { Keyboard } from 'react-native';

在你的代码中可以是这样的:

render() {
    return (
      <TextInput
        onSubmit={Keyboard.dismiss}
      />
    );
  }

静态解散() 解散活动键盘并移除焦点。

其他回答

使用ScrollView而不是View,并将keyboardShouldPersistTaps属性设置为false。

<ScrollView style={styles.container} keyboardShouldPersistTaps={false}>
    <TextInput
        placeholder="Post Title"
        onChange={(event) => this.updateTitle(event.nativeEvent.text)}
        style={styles.default}/>
 </ScrollView>

使用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>

https://facebook.github.io/react-native/docs/keyboard.html

Use

Keyboard.dismiss(0);

隐藏键盘。

在ScrollView中使用keyboardShouldPersistTaps,你可以传入“handled”,它处理人们所说的使用ScrollView带来的问题。这是文档中关于使用'handled'的说明:当点击被子程序处理(或被父程序捕获)时,键盘不会自动解散。这里是引用的地方。

在ScrollView中使用

keyboardShouldPersistTaps="handled" 

这个可以帮到你。