【问题标题】:Input in react native gifted chat is covered by keyboardreact native 天才聊天中的输入被键盘覆盖
【发布时间】:2020-09-06 22:42:39
【问题描述】:

我遇到了 Android 版本的问题。

我正在为我的应用程序聊天使用天才聊天。但是文本输入被键盘覆盖,所以我看不到我在输入什么。

我正在使用 react-native 版本 0.51。我已经遵循了几个解决方案,但它仍然无法正常工作。

我尝试了this solution,它使用了keyboardAvoidingView,还添加了KeyboardSpacer,但它也不起作用。

任何建议都会非常棒。

这是我的渲染组件代码

render() {
console.log(this.state);
return (
  <View style={{flex: 1}}>
    <GiftedChat
      messages={this.state.messages}
      onSend={Fire.shared.send}
      loadEarlier={this.state.loadEarlier}
      isLoadingEarlier={this.state.isLoadingEarlier}

      user={{
        name: this.props.profile.name,
        _id: this.props.profile.user_id,
        avatar: this.props.profile.profile_image
      }}

      renderUsernameOnMessage={true}
      renderActions={this.renderCustomActions}
      renderAvatar={this.renderAvatar}
      renderBubble={this.renderBubble}
      renderSend={this.renderSend}
      renderSystemMessage={this.renderSystemMessage}
      renderCustomView={this.renderCustomView}
      renderFooter={this.renderFooter}
      keyboardShouldPersistTaps={'always'}
    />
    <KeyboardSpacer/>
  </View>
)}

【问题讨论】:

  • 您是否尝试设置keyboardVerticalOffset 值?
  • 是的,我做到了,但它仍然无法正常工作:(

标签: reactjs keyboard chat native


【解决方案1】:

这似乎是 Android 设备上 React Native Gifted Chat 和 Expo 的常见问题。

您可以使用react-native-keyboard-spacer 包在打开键盘后保持内容可见:

import React, { Component } from 'react';
import { View, Platform } from 'react-native';
import KeyboardSpacer from 'react-native-keyboard-spacer';
import { GiftedChat } from 'react-native-gifted-chat';

export default class Chat extends Component {
  render() {
    const giftedChatMessages = [
      ...
    ];
    return (
      <View style={{flex: 1}}>
        <GiftedChat
          messages={giftedChatMessages}
          onSend={newMessages => onSend(newMessages[0].text)}
          user={{
              _id: 1,
          }}
          renderAvatar={() => null}
        />
        {Platform.OS === 'android' ? <KeyboardSpacer /> : null }
      </View>   
    )
  }
}

【讨论】:

    【解决方案2】:

    我也有同样的问题。我通过在 AndroidManifest.xml 文件中添加android:windowSoftInputMode="adjustResize" 来解决它,如下所示:

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      ...
      ...
      >
      <activity
        android:name=".MainActivity"
        ...
        ...
        android:windowSoftInputMode="adjustResize" <!-- add here -->
      >
        ...
      </activity>
      ...
    </application>
    

    【讨论】:

      【解决方案3】:

      这个呢:

      import { KeyboardAvoidingView } from 'react-native';
      
      <View style={{ flex: 1 }}>
        <GiftedChat
          messages={this.state.messages}
          onSend={messages => this.onSend(messages)}
          user={{
            _id: 1,
          }}
        />
        {Platform.OS === 'android' && <KeyboardAvoidingView behavior="padding" />}
      </View>;
      

      【讨论】: