最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - blur <TextInput> by tapping outside of it, doesn't work in react native - Stack Overflow

programmeradmin3浏览0评论

I have <TextInput/> and I would like to get to blur it and toggle the keyboard when tapping outside the <TextInput/> I have tried this solution but without any luck.

import React, { Component } from "react";
import { AppRegistry, Dimensions, StyleSheet, Text, TouchableHighlight, View, Image, TextInput, ScrollView, Keyboard, TouchableWithoutFeedback } from "react-native";
import { Button } from "react-native-elements";

class CounterextendsComponent {
  render() {
    return (
      <View style={styles.container} onPress={this.focusOff}>
        <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}>
          <View>
            <TextInput
              ref="numberInput"
              id={this.props.id}
              style={styles.title}
              keyboardType="numeric"
              maxLength={2}
              value={this.state.keys.toString()}
              onChange={(event) =>
                this.updateKeysWithInputHandler(event.nativeEvent.text)
              }
            />
          </View>
        </TouchableWithoutFeedback>
      </View>
    );
  }
}

I have <TextInput/> and I would like to get to blur it and toggle the keyboard when tapping outside the <TextInput/> I have tried this solution but without any luck.

import React, { Component } from "react";
import { AppRegistry, Dimensions, StyleSheet, Text, TouchableHighlight, View, Image, TextInput, ScrollView, Keyboard, TouchableWithoutFeedback } from "react-native";
import { Button } from "react-native-elements";

class CounterextendsComponent {
  render() {
    return (
      <View style={styles.container} onPress={this.focusOff}>
        <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}>
          <View>
            <TextInput
              ref="numberInput"
              id={this.props.id}
              style={styles.title}
              keyboardType="numeric"
              maxLength={2}
              value={this.state.keys.toString()}
              onChange={(event) =>
                this.updateKeysWithInputHandler(event.nativeEvent.text)
              }
            />
          </View>
        </TouchableWithoutFeedback>
      </View>
    );
  }
}
Share Improve this question edited Apr 23, 2023 at 5:54 Megan 1,1503 gold badges16 silver badges47 bronze badges asked May 7, 2018 at 9:55 obiwankenoobiobiwankenoobi 1,5825 gold badges23 silver badges37 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

You need to pass your dimensions / flex of the parent container to the TouchableWithoutFeedbackin order for it work.

Assuming that you have flex: 1 in your styles.container, add

<TouchableWithoutFeedback style={{flex: 1}} onPress={Keyboard.dismiss} accessible={false}>

to your child ponent in order for it to inherit the flex.

发布评论

评论列表(0)

  1. 暂无评论