【问题标题】:KeyboardAwareScrollView on the button按钮上的 KeyboardAwareScrollView
【发布时间】:2019-01-26 18:30:35
【问题描述】:

我在屏幕底部有一个按钮,在屏幕顶部有一个输入字段。当<TextInput> 聚焦时 - 键盘与按钮重叠,并且在我单击返回按钮之前无法按下它。我希望在打开键盘时将提交按钮向上推,并在键盘未激活时返回屏幕底部。

KeyboardAwareScrollView 与<TextInput/> 配合良好,但似乎不适用于按钮。有什么想法可以实现吗?谢谢!

render() {
    return (
        <KeyboardAwareScrollView
            contentContainerStyle={{
                flex: 1,
                flexDirection: 'column',
                justifyContent: 'flex-end',
                alignItems: 'center',
                backgroundColor: 'skyblue'
            }}
        >

            <View>
                <TextInput placeholder='John'
                           autoFocus={true}/>
                <Button>
                    <Text>Submit</Text>
                </Button>
            </View>
        </KeyboardAwareScrollView>
    )
} 

【问题讨论】:

    标签: react-native


    【解决方案1】:

    KeyboardAwareScrollView 有一个名为 extraScrollHeight 的道具,可用于此目的。

    https://github.com/APSL/react-native-keyboard-aware-scroll-view#props

    extraScrollHeight - 为键盘添加额外的偏移量。如果你想坚持的话很有用 键盘上方的元素。

    您可以将它与onFocus 属性结合起来设置extraScrollHeight,以便键盘保持在按钮下方。

    <KeyboardAwareScrollView
      contentContainerStyle={{
          flex: 1,
          flexDirection: 'column',
          justifyContent: 'flex-end',
          alignItems: 'center',
          backgroundColor: 'skyblue'
      }}
      extraScrollHeight={this.state.extraScrollHeight}>
      <View>
        <TextInput 
          ref={ref => { this.textInput = ref; }}
          placeholder='John'
          onFocus={(event) => {
            this.setState({extraScrollHeight:30})
          }}
          autoFocus={true}
          />
        <Button> 
          <Text>Submit</Text>
        </Button>
      </View>
    </KeyboardAwareScrollView>
    

    这将允许您根据您正在查看的TextInput 动态设置extraScrollHeight。您需要管理每个TextInput 上的extraScrollHeight。

    或者,您可以设置一个extraScrollheight 并保留它。


    键盘上方移动按钮更新

    问题发布者更新了他们的问题,指出 TextInput 位于页面顶部,而按钮位于底部。按钮显示在键盘上方向上移动。

    或者,您可以将侦听器添加到Keyboard,因为这将获得键盘的高度并允许您为按钮设置动画。

    1. import { Keyboard, Animated } from 'react-native'
    2. 为按钮的初始位置设置一个新的Animated.Value。
    3. 在componentDidMount 中为keyboardDidShow 和keyboardDidHide 添加侦听器,并在componentWillUnmount 中删除它们
    4. 将方法添加到_keyboardShow 和_keyboardHide,这将使按钮动画化到键盘高度以上
    5. 在Animated.View 中包含按钮,位置由this.state.initialPosition 设置

    代码如下:

    import * as React from 'react';
    import { View, StyleSheet, Animated, Button, TextInput, Keyboard } from 'react-native';
    import { Constants } from 'expo';
    
    export default class App extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          initialPosition: new Animated.Value(60)
        }
      }
    
      componentDidMount () {
        this.keyboardShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardShow);
        this.keyboardHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardHide);
      }
    
      componentWillUnmount() {
        this.keyboardShowListener.remove();
        this.keyboardHideListener.remove();
      }
    
      _keyboardHide = (e) => {
        Animated.timing(
          this.state.initialPosition,
          {
            toValue: 60
          }
        ).start();
      }
    
      _keyboardShow = (e) => {
        Animated.timing(
          this.state.initialPosition,
          {
            toValue: e.endCoordinates.height
          }
        ).start();
      }
    
      render() {
        return (
          <View style={styles.container}>
            <View style={styles.mainContainer}>
              <TextInput 
                placeholder='Enter first name'
                autoFocus
                style={{fontSize: 24}}
              />
              </View>
    
            <Animated.View style={{bottom: this.state.initialPosition}}>
              <Button 
                onPress={() => alert('submit')} title={'submit'} 
              />
            </Animated.View>
          </View>
        )
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        paddingTop: Constants.statusBarHeight,
      },
      mainContainer: {
        flex: 1,
        alignItems: 'center'
      }
    });
    

    这里是小吃https://snack.expo.io/@andypandy/animated-button-above-keyboard

    值得注意的是

    请注意,如果您将 android:windowSoftInputMode 设置为 adjustResize 或 adjustNothing,只有keyboardDidShow 和keyboardDidHide 事件会 在安卓上可用。 keyboardWillShow 和 keyboardWillHide 是 通常在 Android 上不可用,因为没有原生 对应事件

    https://facebook.github.io/react-native/docs/keyboard#addlistener

    否则我会使用keyboardWillShow 和keyboardWillHide,因为它们会在键盘显示/隐藏之前被调用,从而使动画更加流畅。

    最后的想法

    显然,这是一个概念证明,但它应该让您了解如何完成您想要的。

    为了提高性能,您可以做的一件事是,如果您之前在应用程序中的任何位置显示键盘,则可以捕获键盘的高度并将其保存,以便以后访问。您可以将其保存在redux、AsyncStorage 中,或者只是通过导航将其传递到此屏幕。然后您可以使用TextInput 的onFocus 属性来移动按钮。

    【讨论】:

    • 感谢您的回复。问题是我的 位于屏幕顶部,通常不受键盘影响,但按钮位于底部,因此,这种方法在我的场景中无效。关于如何在我的案例中采用这种方法的任何想法?
    • 您可以在按钮上使用动画,使其向上平移屏幕,使其位于键盘上方。动画可以由 onFocus 事件触发
    • 这可能会很复杂,因为我需要检测键盘高度才能知道我需要将它推到上面多少像素?
    • 我对如何做到这一点有一些想法,并更新了我的答案以显示在键盘上方移动一个按钮。
    • 安德鲁,非常感谢,这是一个很好的答案!
    【解决方案2】:

    不确定这是否仍然相关,但这对我有用。希望这可能会在未来对其他人有所帮助。诀窍是将键盘感知滚动视图放在视图中。您有一个附加到 flex-end 的附加视图。这将始终停留在屏幕底部。

    <View style={{flex: 1, flexDirection:'column'}}>
      <KeyboardAwareScrollView>
        {...things that need to scroll}
      </KeyboardAwareScrollView>
      <View style={{flex-direction:'flex-end'}}>
        {...whatever button or text}
      </View>
    </View>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      • 1970-01-01
      • 2022-07-27
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多