键盘上方的React Native done按钮

qpgpyjmq  于 2023-03-03  发布在  React
关注(0)|答案(5)|浏览(211)

我想在键盘上方添加一个“完成”按钮,单击时将隐藏键盘。
下面的图像演示了所需的按钮:

是否有任何现有的方法或库用于此?(我已经找到this library,但它不工作)。

voj3qocg

voj3qocg1#

对于数字和数字键盘:

并且似乎不需要任何库returnKeyType='done' works with "number-pad" and "numeric" on v0.47.1

对于普通键盘,您可能会看到:

https://github.com/ardaogulcan/react-native-keyboard-accessory
以及
https://github.com/douglasjunior/react-native-keyboard-manager

您需要查看的Github线程:

https://github.com/facebook/react-native/issues/1190
以及
https://github.com/facebook/react-native/issues/641
希望能有所帮助

qyswt5oh

qyswt5oh2#

您可以将React-native的KeyboardAvoidingView组件用作:

<KeyboardAvoidingView keyboardVerticalOffset={50}>
  // View you want to be moved up when keyboard shows.
</KeyboardAvoidingView>

keyboardVerticalOffset={50}是键盘和视图之间的边距,这将是你想要的视图或按钮的高度,我希望这有帮助。
编辑:我认为最好的和最可定制的方式来做到这一点,是听键盘事件和改变的绝对位置的组件,你想要以上的键盘,根据它。

import { Keyboard } from "react-native";
            
componentDidMount () {
  this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', (event) => this.keyboardDidShow(event));
  this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', (event) => this.keyboardDidHide(event));
}
        
keyboardDidShow(event) {
  this.setState({keyboardShow:true,keyboardHeight:event.endCoordinates.height}) // <<You got the keyboard height 
}
        
keyboardDidHide(event) {
  this.setState({keyboardShow:false,keyboardHeight:0})
}
        
componentWillUnmount() {
  this.keyboardDidShowListener.remove();
  this.keyboardDidHideListener.remove();
}

现在,要将其显示在键盘上方,您可以像这样为按钮组件给予样式

style={{
  position: "absolute",
  bottom: this.state.keyboardHeight + 20,
  right: 0
}}

如果您想隐藏它(Done按钮),只需使用keyboardShow状态调整JSX。

gj3fmq9x

gj3fmq9x3#

我在此分享我的办案风格:

    • 代码:**
import React from 'react'
import { StyleSheet, Platform, View, Text, KeyboardAvoidingView, Keyboard } from 'react-native';
import { TextInput } from 'react-native-gesture-handler';
export default class StripAboveKeyboard extends React.Component {

    constructor(props) {
        super(props)
        this.state = { keyboardHeight: 0 }
    }

    componentDidMount() {
        this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', (event) => this.keyboardDidShow(event));
        this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', (event) => this.keyboardDidHide(event));
    }

    keyboardDidShow = (event) => this.setState({ keyboardShow: true, keyboardHeight: event.endCoordinates.height > 100 ? (Platform.OS == 'ios' ? event.endCoordinates.height : 0) : 0 })
    keyboardDidHide = (event) => this.setState({ keyboardShow: false, keyboardHeight: 0 })

    componentWillUnmount() {
        this.keyboardDidShowListener.remove();
        this.keyboardDidHideListener.remove();
    }

    render() {

        marginFromBottom = (this.state.keyboardHeight == 0) ? 0 : this.state.keyboardHeight

        return (
            <KeyboardAvoidingView style={{ flex: 1 }}>
                <View style={style.parent}>
                    <View style={style.upper}>
                        <TextInput style={style.textInput}>User Name</TextInput>
                    </View>
                    <View style={{ ...style.bottomParent, marginBottom: marginFromBottom }}>
                        <Text style={style.bottom}>Press me</Text>
                    </View>
                </View>
            </KeyboardAvoidingView>)
    }
}

const style = StyleSheet.create({
    parent: {
        flex: 1,
        padding: 10,
        backgroundColor: 'pink',
    },

    upper: {
        paddingTop: 44,
        backgroundColor: 'green',
        padding: 10,
        flex: 1,
        marginBottom: 10,
    },

    textInput: {
        height: 40, borderColor: 'gray', borderWidth: 1
    },

    bottomParent: {
        justifyContent: "center",
        alignItems: "center",
        backgroundColor: 'red',
        width: '100%',
        height: 40,
    },

    bottom: {
        textAlignVertical: "center", textAlign: "center",
    }
})
    • 屏幕截图**:
    • 安卓和IOS**

piv4azn7

piv4azn74#

那不是一个库或任何特别的东西。那只是一个随着键盘向上移动的视图。
阅读本文-https://medium.freecodecamp.org/how-to-make-your-react-native-app-respond-gracefully-when-the-keyboard-pops-up-7442c1535580#.gd37tn1wx
它向您展示了使元素尊重键盘的不同方法。

1sbrub3j

1sbrub3j5#

完成按钮不适用于多行TextInput。要关闭键盘,您必须使用

KeyboardAvoidingView

这将有助于通过触摸软键盘的外部来关闭它

相关问题