首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TextInput onPress()

TextInput onPress()
EN

Stack Overflow用户
提问于 2018-07-17 07:50:50
回答 3查看 8.6K关注 0票数 4

我的应用程序中有一个聊天屏幕,在它旁边的button和它的onPress()的帮助下,我发送了一条消息,

以下是代码:

代码语言:javascript
复制
import React, { Component } from "react";
import {
  View,
  Text,
  StyleSheet,
  TextInput,
  Button,
  Image,
  TouchableOpacity,
  Picker,
  AsyncStorage
} from "react-native";
import axios from "axios";
import { Dropdown } from "react-native-material-dropdown";
import { Input } from "native-base";

class MessageForm extends Component {
  state = {
    message: "",
    rc_id: this.props.rc_id,
    usertype: this.props.usertype,
    senderid: this.props.userid,
    subject: this.props.subject
  };

  sendMessage = async () => {
    console.log(
      this.state.rc_id,
      this.state.usertype,
      this.state.senderid,
      this.state.message,
      this.state.subject
    );
    try {
      let { data } = await axios
        .post("https://tgesconnect.org/api/Communicate_class", {
          userid: this.state.rc_id,
          usertype: this.state.usertype,
          message: this.state.message,
          senderid: this.state.senderid,
          subject: this.state.subject
        })
        .then(response => {
          console.log(response.data);
          this.setState({
            message: ""
          });
        });
    } catch (err) {
      console.log(err);
    }
  };

  render() {
    return (
      <View>
        <View style={styles.container}>
          <Input
            style={styles.textInput}
            placeholder={this.props.message}
            underlineColorAndroid="transparent"
            onChangeText={message => {
              this.setState({ message });
            }}
          />

          <TouchableOpacity
            style={styles.button}
            onPress={() => {
              this.sendMessage();
            }}
          >
            <Image source={require("../../Assets/Images/ic_send.png")} />
          </TouchableOpacity>
        </View>
      </View>
    );
  }
}
export default MessageForm;

const styles = StyleSheet.create({
  container: {
    display: "flex",
    flexDirection: "row",
    minWidth: "100%",
    backgroundColor: "#eeeeee",
    borderTopColor: "#cccccc",
    borderTopWidth: 1,
    flex: 1,
    justifyContent: "flex-end"
  },
  textInput: {
    flex: 1,
    backgroundColor: "#ffffff",
    height: 40,
    margin: 10,
    borderRadius: 5,
    padding: 3
  },
  button: {
    flexShrink: 0,
    width: 40,
    height: 40,
    marginTop: 10,
    marginRight: 10,
    marginBottom: 10,
    alignItems: "center",
    justifyContent: "center"
  },
  container1: {
    flex: 1,
    alignItems: "center",
    justifyContent: "center"
  }
});

现在,当我在可触摸图标上单击时,textbox没有被清除,我不明白为什么它不会发生。

我又一次改变了状态,变成了一个空白的文本,但它仍然没有发生。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-07-17 08:02:06

将输入值与状态绑定。

代码语言:javascript
复制
<Input
   style={styles.textInput}
   placeholder={this.props.message}
   underlineColorAndroid="transparent"
   onChangeText={message => {this.setState({ message });}}
   value={this.state.message}
/>
票数 4
EN

Stack Overflow用户

发布于 2021-03-05 22:16:11

您可以使用useState。

只需由import {useState} from "react";导入

代码语言:javascript
复制
const [text, setText] = useState("");
const onChange = (textValue) => setText(textValue);
return (
    <View style={styles.container}>
        <TextInput
            placeholder="Item"
            style={styles.text}
            onChangeText={onChange}
            value={text}
        />
        <TouchableOpacity
            style={styles.btn}
            onPress={() => {
                //addItem.addItem(text);
                setText("");
            }}
        >
            <Text>Add</Text>
        </TouchableOpacity>
    </View>
);
票数 2
EN

Stack Overflow用户

发布于 2020-04-15 16:51:25

代码语言:javascript
复制
           <TextInput
                style={styles.textInput} 
                  placeholder="Start to type.."
                  multiline={true}
                  maxLength={3000}
                  value={this.state.sms}
                  onChangeText={(sms)=>this.setState({sms})}
                />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51375991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档