【问题标题】:react native: use with "KeyboardAwareScrollView" doesnt work反应原生:与“KeyboardAwareScrollView”一起使用不起作用
【发布时间】:2021-01-26 11:25:10
【问题描述】:

我尝试使用来自linkKeyboardAwareScrollView,但它不起作用,不知道为什么。 我还尝试使用“KeyboardAvoidingView”,但没有帮助。 我真的不明白我的问题出在哪里,并且很乐意获得有关该主题的指导或帮助,因为每次键盘都会隐藏文本框并且我不明白如何处理这个问题。

这是我的例子:

import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'


function NetuneyDigum() {
  const navigation = useNavigation();
  const [currentTime, setCurrentTime] = useState('');
  const [isSelected, setSelection] = useState(false);

  const [selectedValue, setSelectedValue] = useState('1');

  return (
    <>
      <View
        style={{
          flex: 1,
          alignItems: 'center',
          backgroundColor: '#cbced4',
        }}
      >
        <View
          style={{
            paddingTop: 30,
            flexDirection: 'row',
            paddingRight: 180,
          }}
        >
          <View
            style={{
              flexDirection: 'row',

              alignItems: 'center',
              justifyContent: 'center',
              left: 58,
            }}
          >
            <Text
              style={{
                fontWeight: 'bold',
                fontSize: 18,
                color: 'black',
              }}
            >
              שעת דיגום:
            </Text>
          </View>
          <View
            style={{
              width: 150,
              borderRadius: 5,
              borderColor: 'black',
              borderWidth: 2,
              left: 80,
              alignItems: 'center',
              justifyContent: 'center',
              backgroundColor: 'white',
            }}
          >
            <Text
              style={{
                fontWeight: 'bold',
                fontSize: 20,
                color: 'black',
              }}
            >
              {currentTime}
            </Text>
          </View>
          <TouchableOpacity
            onPress={() => {
              const time = moment().format('HH:mm');
              setCurrentTime(time);
            }}
            style={{
              height: 60,
              width: 60,
              borderRadius: 5,
              borderColor: 'black',
              borderWidth: 2,
              alignItems: 'center',
              justifyContent: 'center',
              backgroundColor: '#275d9f',
              left: 100,
            }}
          >
            <Icon size={40} color="white" name="time-outline" />
          </TouchableOpacity>
        </View>
        <View
          style={{
            flexDirection: 'row',
            alignSelf: 'flex-start',
            // alignItems: 'flex-start',
            top: 40,
            left: 10,
          }}
        >
          <CheckBox
            value={isSelected}
            onValueChange={setSelection}
            style={styles.checkbox}
            tintColors={{ true: 'blue', false: 'black' }}
          />
          <Text style={styles.label}>שאיבת חוץ</Text>
        </View>
        <View
          style={{
            alignSelf: 'center',
            backgroundColor: '#275d9f',
            height: 50,
            width: 400,
            borderRadius: 5,
            borderColor: 'black',
            borderWidth: 2,
            top: 50,
          }}
        >
          <Text style={styles.Notice}>הערות</Text>
        </View>

        <KeyboardAwareScrollView
          behaviour="position"
          style={styles.container}
          enabled
        >
          <TextInput
            style={{
              top: 50,
              alignSelf: 'center',
              height: 200,
              width: 400,
              backgroundColor: 'white',
              borderWidth: 2,
              borderRadius: 5,
              fontSize: 18,
            }}
            placeholder="כתוב כאן.."
            // onChangeText={(text) => onChangeText(text)}
            // value={value5}
          />
        </KeyboardAwareScrollView>

        <View
          style={{
            flexDirection: 'row',
            alignItems: 'flex-end',
            alignSelf: 'flex-start',
            bottom: 70,
            // paddingTop: 50,
          }}
        >
          <Text style={styles.label}>מעבדה:</Text>
          <View
            style={{
              borderWidth: 2,
              borderRadius: 5,
              backgroundColor: 'white',
              left: 15,
            }}
          >
            <Picker
              mode="dropdown"
              selectedValue={selectedValue}
              style={{
                placeholderTextColor: 'black',
                height: 50,
                width: 220,
                justifyContent: 'flex-end',
                transform: [{ scaleX: 1.2 }, { scaleY: 1.2 }],
                left: 15,
              }}
              onValueChange={(itemValue, itemIndex) =>
                setSelectedValue(itemValue)
              }
            >
              <Picker.Item label="ראש העין" value="1" />
              <Picker.Item label="מעבדה חיפה" value="2" />
              <Picker.Item label="מעבדה נס ציונה" value="3" />
            </Picker>
          </View>
        </View>
      </View>
    </>
  );
}

【问题讨论】:

    标签: javascript reactjs react-native keyboard


    【解决方案1】:

    尝试将所有元素包装在 KeyboardAwareScrollView 中,而不仅仅是 TextInput。 尝试使用这样的东西:

    function NetuneyDigum() {
      const navigation = useNavigation();
      const [currentTime, setCurrentTime] = useState('');
      const [isSelected, setSelection] = useState(false);
    
      const [selectedValue, setSelectedValue] = useState('1');
    
      return (
        <KeyboardAwareScrollView behaviour="position" enabled>
          <View
            style={{
              paddingTop: 30,
              flexDirection: 'row',
              paddingRight: 180,
            }}>
            <View
              style={{
                flexDirection: 'row',
    
                alignItems: 'center',
                justifyContent: 'center',
                left: 58,
              }}>
              <Text
                style={{
                  fontWeight: 'bold',
                  fontSize: 18,
                  color: 'black',
                }}>
                שעת דיגום:
              </Text>
            </View>
            <View
              style={{
                width: 150,
                borderRadius: 5,
                borderColor: 'black',
                borderWidth: 2,
                left: 80,
                alignItems: 'center',
                justifyContent: 'center',
                backgroundColor: 'white',
              }}>
              <Text
                style={{
                  fontWeight: 'bold',
                  fontSize: 20,
                  color: 'black',
                }}>
                {currentTime}
              </Text>
            </View>
            <TouchableOpacity
              onPress={() => {
                const time = moment().format('HH:mm');
                setCurrentTime(time);
              }}
              style={{
                height: 60,
                width: 60,
                borderRadius: 5,
                borderColor: 'black',
                borderWidth: 2,
                alignItems: 'center',
                justifyContent: 'center',
                backgroundColor: '#275d9f',
                left: 100,
              }}>
              <Icon size={40} color="white" name="time-outline" />
            </TouchableOpacity>
          </View>
          <View
            style={{
              flexDirection: 'row',
              alignSelf: 'flex-start',
              // alignItems: 'flex-start',
              top: 40,
              left: 10,
            }}>
            <CheckBox
              value={isSelected}
              onValueChange={setSelection}
              style={styles.checkbox}
              tintColors={{ true: 'blue', false: 'black' }}
            />
            <Text style={styles.label}>שאיבת חוץ</Text>
          </View>
          <View
            style={{
              alignSelf: 'center',
              backgroundColor: '#275d9f',
              height: 50,
              width: 400,
              borderRadius: 5,
              borderColor: 'black',
              borderWidth: 2,
              top: 50,
            }}>
            <Text style={styles.Notice}>הערות</Text>
          </View>
    
          <TextInput
            style={{
              top: 50,
              alignSelf: 'center',
              height: 200,
              width: 400,
              backgroundColor: 'white',
              borderWidth: 2,
              borderRadius: 5,
              fontSize: 18,
            }}
            placeholder="כתוב כאן.."
            // onChangeText={(text) => onChangeText(text)}
            // value={value5}
          />
    
          <View
            style={{
              flexDirection: 'row',
              alignItems: 'flex-end',
              alignSelf: 'flex-start',
              bottom: 70,
              // paddingTop: 50,
            }}>
            <Text style={styles.label}>מעבדה:</Text>
            <View
              style={{
                borderWidth: 2,
                borderRadius: 5,
                backgroundColor: 'white',
                left: 15,
              }}>
              <Picker
                mode="dropdown"
                selectedValue={selectedValue}
                style={{
                  placeholderTextColor: 'black',
                  height: 50,
                  width: 220,
                  justifyContent: 'flex-end',
                  transform: [{ scaleX: 1.2 }, { scaleY: 1.2 }],
                  left: 15,
                }}
                onValueChange={(itemValue, itemIndex) =>
                  setSelectedValue(itemValue)
                }>
                <Picker.Item label="ראש העין" value="1" />
                <Picker.Item label="מעבדה חיפה" value="2" />
                <Picker.Item label="מעבדה נס ציונה" value="3" />
              </Picker>
            </View>
          </View>
        </KeyboardAwareScrollView>
      );
    }
    

    【讨论】:

    • 你能说出你的意思吗?在我的代码中展示你的例子,我会看看它是否有效。
    • 我包装了所有代码,现在它在我的屏幕元素中混合了一些内容
    • 你是什么意思it make some mix in my screen
    • 现在键盘在输入文字的情况下只能隐藏一半,我该如何解决?
    猜你喜欢
    • 2018-01-02
    • 1970-01-01
    • 2019-09-13
    • 2019-09-11
    • 1970-01-01
    • 2021-06-23
    • 2021-03-12
    • 2017-02-16
    • 1970-01-01
    相关资源
    最近更新 更多