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

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

当前回答

如果有人需要一个工作的例子,如何解散一个多行文本输入在这里ya !希望这能帮助一些人,文档没有描述一种方法来消除多行输入,至少没有具体的参考如何做到这一点。仍然是一个在堆栈上实际发帖的新手,如果有人认为这应该是对这个片段所写的实际帖子的引用,请告诉我。

import React, { Component } from 'react'
import {
  Keyboard,
  TextInput,
  TouchableOpacity,
  View,
  KeyboardAvoidingView,
} from 'react-native'

class App extends Component {
  constructor(props) {
    super(props)
    this.state = {
      behavior: 'position',
    }
    this._keyboardDismiss = this._keyboardDismiss.bind(this)
  }

  componentWillMount() {
    this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide);
  }

  componentWillUnmount() {
    this.keyboardDidHideListener.remove()
  }

  _keyboardDidHide() {
    Keyboard.dismiss()
  }

  render() {
    return (
      <KeyboardAvoidingView
        style={{ flex: 1 }}
        behavior={this.state.behavior}
      >
        <TouchableOpacity onPress={this._keyboardDidHide}>
          <View>
            <TextInput
              style={{
                color: '#000000',
                paddingLeft: 15,
                paddingTop: 10,
                fontSize: 18,
              }}
              multiline={true}
              textStyle={{ fontSize: '20', fontFamily: 'Montserrat-Medium' }}
              placeholder="Share your Success..."
              value={this.state.text}
              underlineColorAndroid="transparent"
              returnKeyType={'default'}
            />
          </View>
        </TouchableOpacity>
      </KeyboardAvoidingView>
    )
  }
}

其他回答

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

使用该组件作为根组件

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

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

使用React Native的Keyboard.dismiss()

更新后的答案

React Native在键盘上暴露了静态的dismiss()方法,因此更新的方法是:

import { Keyboard } from 'react-native'; 

Keyboard.dismiss()

原来的答案

使用React Native的遣散键盘库。

我有一个非常相似的问题,感觉我是唯一一个没有得到它。

ScrollViews

如果你有一个ScrollView,或者任何从它继承的东西,比如ListView,你可以添加一个道具,它会根据按下或拖动事件自动取消键盘。

道具是keyboardDismissMode,值可以为none、interactive或on-drag。你可以在这里阅读更多。

常规的观点

如果你有一个ScrollView以外的东西,你想要任何按键来解除键盘,你可以使用一个简单的TouchableWithoutFeedback,并让onPress使用React Native的实用程序库来解除键盘。

在你的例子中,你可以这样做:

var DismissKeyboard = require('dismissKeyboard'); // Require React Native's utility library.

// Wrap your view with a TouchableWithoutFeedback component like so.

<View style={styles.container}>

  <TouchableWithoutFeedback onPress={ () => { DismissKeyboard() } }>

    <View>

      <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}} />

    </View>

  </TouchableWithoutFeedback>

</View>

注意:TouchableWithoutFeedback只能有一个子元素,所以你需要把它下面的所有元素都包装在一个视图中,如上所示。

使用keyboard .dismiss()在任何时候解散键盘。

我是React的新手,在制作演示应用程序时遇到了完全相同的问题。如果你使用onStartShouldSetResponder道具(此处描述),你可以在一个普通的旧React. view上抓取触摸。很想听听更多经验丰富的反应者对这一策略的看法/如果有更好的策略,但这对我来说是有效的:

containerTouched(event) {
  this.refs.textInput.blur();
  return false;
}

render() {
  <View onStartShouldSetResponder={this.containerTouched.bind(this)}>
    <TextInput ref='textInput' />
  </View>
}

这里需要注意两点。首先,正如这里讨论的,还没有结束所有子视图编辑的方法,所以我们必须直接引用TextInput来模糊它。其次,onStartShouldSetResponder被它上面的其他可触摸控件截获。因此,在容器视图中点击TouchableHighlight等(包括另一个TextInput)将不会触发事件。但是,在容器视图中单击Image仍然会关闭键盘。

如何在TextInput周围/旁边放置一个可触摸组件?

var INPUTREF = 'MyTextInput';

class TestKb extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <View style={{ flex: 1, flexDirection: 'column', backgroundColor: 'blue' }}>
                <View>
                    <TextInput ref={'MyTextInput'}
                        style={{
                            height: 40,
                            borderWidth: 1,
                            backgroundColor: 'grey'
                        }} ></TextInput>
                </View>
                <TouchableWithoutFeedback onPress={() => this.refs[INPUTREF].blur()}>
                    <View 
                        style={{ 
                            flex: 1, 
                            flexDirection: 'column', 
                            backgroundColor: 'green' 
                        }} 
                    />
                </TouchableWithoutFeedback>
            </View>
        )
    }
}