在TextInput中设置文本react-native

时间:2018-01-29 18:05:44

标签: react-native

有没有办法再次设置react-native的textinput中的文本值? 如下所示,这是我做的: 我想在textinput中设置我的电源转换器文本值。我可以这样做吗?

   <TextInput 
         style={{color:'black',width:width-60, borderColor: 'black'}}
         placeholder="Ask your Question ?"
         underlineColorAndroid='black'
         autoCapitalize="words"
         placeholderTextColor="black"
         keyboardType = "default"
         error = {this.state.errpwd}
         onBlur={() => this.setState({errpwd:""})}
         onChangeText={ (questionText) => this.setState({input: questionText}) }/>      

          <View style={styles.section}>
                <PowerTranslator style={styles.p} text={'user input: ' + this.state.input} />
            </View>

3 个答案:

答案 0 :(得分:1)

为TextInput

添加值prop
value={this.state.input}

答案 1 :(得分:0)

从'react'导入React,{组件}; 导入{   文本,   查看

}来自“ react-native”;

导出默认类App扩展组件{   构造函数(道具){     超级(道具)

this.state = {
  refreshing: false,

  t: [],

  txt:'Helloo',  //enter your text here

}    console.log(this.state.t);

}

render(){

return (
  <View>
    <TextInput
     controlled={true}
      onChangeText={(txt) => this.setState({ txt })}
      value={this.state.txt}
      style={{ width: '90%', elevation: 10, height: '16%', backgroundColor: 'white', alignSelf: 'center', borderBottomRightRadius: 10, margin: 4 }}></TextInput>
  </View>
);

} }

答案 2 :(得分:0)

构造函数(道具){

super(props)

this.state = {

  txt: 'ENter Number',    //write here your Text 

  Array: []
}

}

点击=()=> {

var bB= this.state.Array       //object of Array            

for (i = 1; i <= this.state.txt; ++i) { 

  this.setState({i:Array})  // push value in array how much User Enter

enter code here bB.push(i)     }
  }

render(){

return (

  <View style={{ flex: 1 }}>

    <TextInput

      controlled={true}
      onChangeText={(txt) => this.setState({ txt })}
      value={this.state.txt}>

    <TouchableOpacity
      onPress={this.click}>

      <Text style={{ color: 'white' }} >Add</Text>

    </TouchableOpacity>

     <FlatList

         ref={ref => this.scrollView = ref}
        refreshing={this.state.refreshing}
        onRefresh={this.onRefresh}
        data={this.state.Array}
        extraData={this.state}
        keyExtractor={this._keyExtractor}
        renderItem={({ item }) =>

        <TouchableOpacity>

            <Text style={{ color: 'white' }} >{item}</Text>

            </TouchableOpacity>
        }
      />
  </View>
);

}