简体   繁体   English

如何使本机基础模态与 Android 中的 KeyboardAvoildingView 一起工作?

[英]How to make native-base Modal works well with KeyboardAvoildingView in Android?

I try to add KeyboardAvoidingView to the Modal Component.我尝试将 KeyboardAvoidingView 添加到模态组件。 But when i call the keyboard up, the modal doesnt move and still be covered by keyboard.但是当我调用键盘时,模态不会移动并且仍然被键盘覆盖。

This is my code: https://snack.expo.dev/@tikkhun/joyous-blueberries这是我的代码: https://snack.expo.dev/@tikkhun/joyous-blueberries

After searching and asking.经过搜索和询问。 I get a way to work well: just use behavior: "position"我有一种很好的工作方式:只需使用行为:“位置”

Here is My example Component:这是我的示例组件:

/**
 * @file: 弹出框
 */
import React, { useRef, useEffect, useState } from 'react';
import { Center, Button, HStack, Input, KeyboardAvoidingView, Modal, Spacer, Text } from 'native-base';
import { useTranslation } from 'react-i18next';

export default function ModalContent({ isOpen, onClose, title, defaultValue, type = 'input', onSave }) {
  const { t } = useTranslation();
  const [value, setValue] = useState();
  const inputRef = useRef(null);
  useEffect(() => {
    // 这里的 setTimeout 是为了让键盘正常弹出
    setTimeout(() => {
      if (inputRef?.current) {
        inputRef.current.focus();
      }
    }, 10);
  });
  useEffect(() => {
    setValue(defaultValue);
    return () => {
      setValue('');
    };
  });

  return (
    <Modal isOpen={isOpen} onClose={onClose}>
      <KeyboardAvoidingView style={{ width: '100%' }} behavior="position">
        <Center>
          <Modal.Content style={{ width: '100%' }}>
            <Modal.Header>
              <HStack space="3" alignItems="center">
                <Text fontSize="md">{title}</Text>
                <Spacer />
                <Button
                  _text={{ fontSize: 'md' }}
                  variant="ghost"
                  onPress={() => {
                    onSave && onSave(value);
                  }}>
                  {t('settings.save')}
                </Button>
              </HStack>
            </Modal.Header>
            <Modal.Body>
              <Input size="2xl" ref={inputRef} defaultValue={value} onChangeText={v => setValue(v)} />
            </Modal.Body>
          </Modal.Content>
        </Center>
      </KeyboardAvoidingView>
    </Modal>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM