React Native实现点击TextInput唤起Date Picker的方法
解决TextInput点击唤起日期选择器的问题
你的问题核心是React Native的TextInput组件不支持onclick属性,需要替换为框架提供的正确属性,同时为了避免点击时弹出不必要的软键盘,还需补充配置项,具体修改方案如下:
基础实现(禁止输入仅唤起选择器)
<TextInput style={styles.forminput} label=" Date of Birth" value="" onPress={displayDatepicker} // 替换错误的onclick为onPress editable={false} // 禁止编辑,避免弹出系统键盘 showSoftInputOnFocus={false} // 确保聚焦时不触发软键盘 />
进阶实现(允许编辑但点击唤起选择器)
如果需要保留TextInput的编辑能力,同时点击时唤起日期选择器,可以改用onFocus事件:
<TextInput style={styles.forminput} label=" Date of Birth" value="" onFocus={displayDatepicker} // 聚焦时唤起日期选择器 showSoftInputOnFocus={false} // 阻止软键盘弹出 />
内容的提问来源于stack exchange,提问作者vivek kn
相关产品推荐
相关产品推荐

