Vấn đề với bàn phím không loại bỏ trở nên nghiêm trọng hơn nếu bạn có keyboardType='numeric', vì không có cách nào để loại bỏ nó.
Thay thế View bằng ScrollView không phải là một giải pháp chính xác, vì nếu bạn có nhiều textInputs hoặc buttons, chạm vào chúng trong khi bàn phím lên sẽ chỉ loại bỏ bàn phím.
Cách chính xác là đóng gói View với TouchableWithoutFeedbackvà gọiKeyboard.dismiss()
EDIT: Bây giờ bạn có thể sử dụng ScrollViewvới keyboardShouldPersistTaps='handled'chỉ bỏ bàn phím khi vòi nước không được xử lý bởi các trẻ em (tức là khai thác trên textInputs hoặc các nút khác.)
Nếu bạn có
<View style={{flex: 1}}>
<TextInput keyboardType='numeric'/>
</View>
Thay đổi nó thành
<ScrollView contentContainerStyle={{flexGrow: 1}}
keyboardShouldPersistTaps='handled'
>
<TextInput keyboardType='numeric'/>
</ScrollView>
hoặc là
import {Keyboard} from 'react-native'
<TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}>
<View style={{flex: 1}}>
<TextInput keyboardType='numeric'/>
</View>
</TouchableWithoutFeedback>
EDIT: Bạn cũng có thể tạo Thành phần bậc cao hơn để loại bỏ bàn phím.
import React from 'react';
import { TouchableWithoutFeedback, Keyboard, View } from 'react-native';
const DismissKeyboardHOC = (Comp) => {
return ({ children, ...props }) => (
<TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}>
<Comp {...props}>
{children}
</Comp>
</TouchableWithoutFeedback>
);
};
const DismissKeyboardView = DismissKeyboardHOC(View)
Đơn giản chỉ cần sử dụng nó như thế này
...
render() {
<DismissKeyboardView>
<TextInput keyboardType='numeric'/>
</DismissKeyboardView>
}
LƯU Ý: accessible={false}bắt buộc phải làm cho biểu mẫu đầu vào tiếp tục có thể truy cập được thông qua VoiceOver. Những người khiếm thị sẽ cảm ơn bạn!