React Native中如何在键盘弹出时显示TextInput下方的组件?

4

当键盘打开时,我希望兴趣文本框下方的标签显示出来。芯片组件是从React Native Paper库中输入的标签。我尝试过使用scrollviewkeyboardavoidingviewkeyboardawarescrollview等视图来让标签在键盘打开时显示,但是没有一个视图可以解决这个问题。我可能遗漏了什么或做错了什么?

  GetTags = () => {
    const tagsComponent = [];
    if (typeof this.state.interests !== "undefined") {
      for (let i = 0; i < this.state.interests.length; i++) {
        tagsComponent.push(
          <Chip
            key={i}
            style={{ margin: 3 }}
            onClose={() => {
              this.setState(prevState => ({
                interests: prevState.interests.filter(
                  x => x !== prevState.interests[i]
                )
              }));
            }}
          >
            {this.state.interests[i]}
          </Chip>
        );
      }
    }
    return tagsComponent;
  };

  render() {
    const displayTags = this.GetTags();
    return (
      <ScrollView style={{ marginTop: 20 }} keyboardShouldPersistTaps="always">
        <View
          style={{
            flex: 1,
            alignItems: "center",
            justifyContent: "center"
          }}
        >
          {this.state.profile ? (
            <Avatar
              size="xlarge"
              rounded
              source={{ uri: this.state.profile }}
              showEditButton
              onEditPress={() => this.onChooseImageUpload()}
            />
          ) : (
            <Avatar
              {...this.state.avatarError}
              size="xlarge"
              rounded
              icon={{
                name: "user",
                type: "font-awesome"
              }}
              showEditButton
              onEditPress={() => this.onChooseImageUpload()}
            />
          )}
          <Text
            style={{
              fontWeight: "500",
              fontSize: 20,
              marginTop: 8,
              marginRight: "70%",
              color: "#5b5b5b"
            }}
          >
            About me
          </Text>
          <TextInput
            style={{
              borderColor: Themes.layoutTheme,
              borderWidth: 1,
              width: "100%",
              marginTop: 5,
              padding: 5,
              textAlignVertical: "top"
            }}
            multiline
            numberOfLines={7}
            placeholder="Type something about yourself..."
            onChangeText={text => this.setState({ bio: text })}
            value={this.state.bio}
          />
          <View style={{ flexDirection: "row" }}>
            <Text
              style={{
                fontWeight: "500",
                fontSize: 20,
                marginTop: 22,
                color: "#5b5b5b"
              }}
            >
              Interests
            </Text>
            <RNPTextInput
              ref={input}
              style={{
                height: 45,
                marginTop: 10,
                marginRight: 82,
                marginLeft: 10
              }}
              underlineColor={Themes.primaryTheme}
              placeholder="Example: Skydiving,"
              theme={{ colors: { primary: Themes.primaryTheme } }}
              onChangeText={text => {
                if (text.endsWith(",")) {
                  const newText = text.replace(",", "");
                  if (typeof this.state.interests === "undefined") {
                    this.setState({ interests: [newText] });
                  } else {
                    this.setState(prevState => ({
                      interests: [...prevState.interests, newText]
                    }));
                  }
                  input.current.clear();
                }
              }}
            />
          </View>
          <View
            style={{
              marginTop: 15,
              flexDirection: "row",
              flexWrap: "wrap",
              alignItems: "flex-start"
            }}
          >
            {displayTags}
          </View>
          {this.state.loading ? (
            <Progress.Bar
              style={{ marginTop: 25 }}
              indeterminate
              color={Themes.primaryTheme}
            />
          ) : (
            <Button
              raised
              containerStyle={{
                width: "25%",
                marginTop: 20
              }}
              buttonStyle={{ backgroundColor: Themes.primaryTheme }}
              title="Save"
              onPress={() => {
                if (fireStoreDB.getAvatar == null) {
                  this.setState({
                    avatarError: {
                      containerStyle: { borderWidth: 1, borderColor: "red" }
                    }
                  });
                } else {
                  fireStoreDB
                    .updateProfile(this.state.bio, this.state.interests)
                    .then(() => this.props.navigation.navigate("Home"));
                }
              }}
            />
          )}
        </View>
      </ScrollView>
    );
  }


尝试使用以下代码:<KeyboardAvoidingView behavior="height" enabled style={{ justifyContent: "flex-end" }} /> - Sathvik Nasani
@SathvikNasani 那没解决问题。我把它添加到了scrollview的顶部,行为与之前相同,直到我添加了更多标签,这时键盘和兴趣textinput之间会添加一些空白空间。 - CLUTCHER
1个回答

0

我通过将content包裹在带有padding行为的KeyboardAvoidingView中,并在文本输入框上方显示芯片来解决了这个问题。

<KeyboardAvoidingView style={{ flex: 1 }} behavior="padding">
  <Content>
    <View style={{ marginTop: "7%" }} />
    {this.state.profile ? (
      <Avatar
        size="xlarge"
        rounded
        containerStyle={{ alignSelf: "center" }}
        source={{ uri: this.state.profile }}
        showEditButton
        onEditPress={() => this.onChooseImageUpload()}
      />
    ) : (
      <Avatar
        {...this.state.avatarError}
        size="xlarge"
        rounded
        containerStyle={{ alignSelf: "center" }}
        icon={{
          name: "user",
          type: "font-awesome"
        }}
        showEditButton
        onEditPress={() => this.onChooseImageUpload()}
      />
    )}
    <Text
      uppercase={false}
      style={{
        fontWeight: "500",
        fontSize: 20,
        marginTop: "6%",
        marginLeft: "1%",
        color: "#5b5b5b"
      }}
    >
      About me
    </Text>
    <TextInput
      style={{
        borderColor: Themes.layoutTheme,
        borderWidth: 1,
        width: "100%",
        marginTop: 5,
        padding: 5,
        textAlignVertical: "top"
      }}
      multiline
      numberOfLines={7}
      placeholder="Type something about yourself..."
      onChangeText={text => this.setState({ bio: text })}
      value={this.state.bio}
    />
    <Text
      style={{
        fontWeight: "500",
        fontSize: 20,
        marginTop: 22,
        marginLeft: "1.5%",
        color: "#5b5b5b"
      }}
    >
      Interests
    </Text>
    <View
      style={{
        marginTop: 15,
        flexDirection: "row",
        flexWrap: "wrap",
        alignItems: "flex-start"
      }}
    >
      {displayTags}
    </View>
    <Input
      ref={input}
      style
      inputContainerStyle={{
        borderBottomColor: Themes.layoutTheme
      }}
      placeholder="What, are, your, interests"
      onChangeText={text => {
        if (text.endsWith(",")) {
          const newText = text.replace(",", "");
          if (typeof this.state.interests === "undefined") {
            this.setState({ interests: [newText] });
          } else {
            this.setState(prevState => ({
              interests: [...prevState.interests, newText]
            }));
          }
          input.current.clear();
        }
      }}
    />
  </Content>
</KeyboardAvoidingView>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接