【问题标题】:Input react native - press in crashes application输入反应本机 - 在崩溃应用程序中按下
【发布时间】:2017-06-14 12:12:10
【问题描述】:

长按输入文字3秒,提示“应用程序名称已停止”,如何纠正?...................... ..................................................... .....................

我的组件

return (
        <ReactNative.TextInput
            ref={(ref: any) => { this.input = ref; }}
            style={styleInputFormDefault}
            numberOfLines={this.state.numberOfLines}
            blurOnSubmit={true}
            editable={this.state.editable}
            underlineColorAndroid={"transparent"}
            value={this.state.value}
            multiline={this.state.multiline}
            placeholder={this.state.placeholder}
            keyboardType="default"
            onChange={event => {
                this.value = event.nativeEvent.text;
            }}
            onEndEditing={event => {
                this.value = event.nativeEvent.text;
                if (this.props.onChange != undefined) {
                    !this.props.onChange(this.value);
                }
            }}
            returnKeyType={this.state.returnKeyType}
            onSubmitEditing={() => {
                if (this.props.onSubmit != undefined) {
                    this.props.onSubmit(this);
                }
            }}
            onFocus={() => {
                if (this.props.onFocus != undefined) {
                    this.props.onFocus();
                };
            }}
            onBlur={() => {
                if (this.props.onBlur != undefined) {
                    this.props.onBlur();
                };
            }}
        >
        </ReactNative.TextInput>

    );

【问题讨论】:

    标签: javascript android reactjs react-native


    【解决方案1】:
    【解决方案2】:

    为什么要这样使用 TextInput 组件?只需从 react native 导入 TextInput。

    试试这个代码:

    import React, { Component } from 'react'
    import { TextInput } from 'react-native'
    
    export default class InputClassName extends Component {
      constructor(props) {
        super(props)
        this.input = null
        this.state {
          [...]
        }
      }
    
      render() {
        return(
          <View>
            <TextInput
                 ref={ref => this.input = ref}
                 style={styleInputFormDefault}
                 numberOfLines={this.state.numberOfLines}
                 blurOnSubmit={true}
                 underlineColorAndroid={"transparent"}
                 value={this.state.value}
                 multiline={this.state.multiline}
                 placeholder={this.state.placeholder}
                 keyboardType="default"
                 onChangeText={value => this.value = value}
                 onEndEditing={event => {
                     // I do not know what you're trying to do here
                    // Are you checking if the onChange props is a function? If so, do this instead:
                    // if("function" === typeof this.props.onChange) { [...] } 
                     this.value = event.nativeEvent.text;
                     if (this.props.onChange != undefined) {
                         !this.props.onChange(this.value);
                     }
                 }}
                 returnKeyType={this.state.returnKeyType}
                 onSubmitEditing={() => {
                     // same goes here
                     if (this.props.onSubmit != undefined) {
                         this.props.onSubmit(this);
                     }
                 }}
                 onFocus={() => {
                     // also here
                     if (this.props.onFocus != undefined) {
                         this.props.onFocus();
                     };
                 }}
                 onBlur={() => {
                     // and here.
                     if (this.props.onBlur != undefined) {
                         this.props.onBlur();
                     };
                 }}
             >
             </TextInput>
           { /* Please note that you can also use propTypes in order to force (recommended)
              a specific prop to be the typeof whatever you want instead of using if/else */ }
          </View>
        )
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-18
      • 2022-12-12
      • 2018-05-05
      • 2022-06-23
      • 1970-01-01
      • 2019-04-15
      • 2021-04-01
      • 1970-01-01
      相关资源
      最近更新 更多