动态将文本添加到TextInput

时间:2019-04-04 07:19:05

标签: react-native

当我单击按钮时,我想添加一个在textInput内部查看的UnicodeText。

我尝试创建状态{text},然后将UnicodeText添加到状态文本。

带有添加的文本的文本正确显示在console.log()中。不在textInput中。

import { Icon, Input } from "react-native-elements";
var emoji = require("node-emoji");

export default class MainViewMessageInput extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      text: "",
      username: "",
      visible: false,
      showEmoticons: false
    };
  }

  _sendMessage() {
    var texte = this.state.text;
    this.setState({ text: "" });
    console.log(this.state.text);
  }

  _addEmoji() {
    this.state.text = this.state.text + emoji.get("green_heart");
    const emojiChar = this.state.text + emoji.get("green_heart");
    this.setState({ text: emojiChar });
    console.log(this.state.text);
  }
  render() {
    return (
      <View style={styles.container}>
        <Input
          style={{ flex: 1 }}
          leftIcon={
            <TouchableHighlight
              onPress={() => {
                this._addEmoji();
              }}
              style={styles.icon}
            >
              <Icon size={40} color="#d2d3d5" name="mood" />
            </TouchableHighlight>
          }
          rightIcon={
            <Icon
              reverse
              onPress={() => this.setState({ visible: true })}
              color="#00b5ec"
              name="send"
              size={23}
            />
          }
          rightIconContainerStyle={{ marginRight: -7 }}
          leftIconContainerStyle={{ marginLeft: 0 }}
          inputContainerStyle={styles.inputContainer}
          inputStyle={styles.input}
          placeholder="Write your message ..."
          underlineColorAndroid="transparent"
          multiline={true}
          editable={true}
          onChangeText={text => this.setState({ text })}
        />
      </View>
    );
  }
}

有可能吗?

3 个答案:

答案 0 :(得分:1)

对于输入添加字段value = {this.state.text}

reference

答案 1 :(得分:0)

您可以在运行时将textinput推送到数组中,以便您动态获取textinput

答案 2 :(得分:0)

this.state.text = this.state.text + emoji.get("green_heart");删除_addEmoji()