代码之家  ›  专栏  ›  技术社区  ›  MoKhajavi75

响应文本输入值的本机单元

  •  0
  • MoKhajavi75  · 技术社区  · 8 年前

    我想把单位加到 textInput 价值:

    enter image description here

    我试着用 value = { this.state.totalWeight + " Kgs"} 但没有结果!

    我还试图添加 " Kgs" 在里面 onSubmitEditing = { () => { this.refs.firstInput.refs.value = this.state.totalWeight + " Kgs" } 但又一次,没有结果!

    知道吗?

    提前谢谢!

    2 回复  |  直到 8 年前
        1
  •  2
  •   Singh    8 年前
    state = {
       value: "",
       unit: "Kgs"
    }
    render(){
       return(
         <View style={{flexDirection: 'row', alignItems: 'center'}} >
                <TextInput value={this.state.value} onChangeText={(value) => this.setState({value})} />
                <View style={{marginRight: 10}} >
                    <Text>{this.state.value === '' ? '' : this.state.unit}</Text>
                </View>
         </View>       
       );
    }
    
        2
  •  0
  •   MoKhajavi75    8 年前

    所以我用这种方式!

    我的 state :

    this.state = {
      commodity: "",
      isFilled: false
    };
    

    然后我们有:

    <TextInput
      style={{ flex: 1 }}
      onChangeText={commodity =>
        this.setState({
          commodity,
          isFilled: false
        })
      }
      value={
        this.state.isFilled ? this.state.commodity + " Unit" : this.state.commodity
      }
      onFocus={() => this.setState({ isFilled: false })}
      onEndEditing={() => {
        if (this.state.commodity == "") {
          this.setState({ isFilled: false });
        } else {
          this.setState({ isFilled: true });
        }
      }}
      placeholder="Commodity"
    />;
    

    现在每次用户输入一个文本,编辑后,单位将被添加。