我正在开发一款React Native应用,希望在按下文本输入框后调用日期选择器,在选择日期后将其显示在文本输入框上。
我正在开发一款React Native应用,希望在按下文本输入框后调用日期选择器,在选择日期后将其显示在文本输入框上。
将TextInput
包装到一个视图中,并将pointerEvents
设置为none
。
现在,您可以使用react-native
中的Pressable
组件来监听onpress
事件。
<Pressable onPress={() => alert('Hi!')}>
<View pointerEvents="none">
<TextInput />
</View>
</Pressable>
您不能直接调用 TextInput
的 onPress
。您只能使用onFocus
,当您按下输入框以将光标移到那里时,会调用它。 如果您的用例不需要,请勿关注 onBlur。如有可能,请在onFocus
内处理关闭。
onFocus = () => {
// do something
}
render() {
<TextInput onFocus={onFocus} />
}
readonly
属性使文本输入字段变为只读。 editable={false}
pointerEvents="none"
将prop添加到TextInput中。
<TextInput
onFocus={this.onFocus}
onBlur={this.onBlur}
/>
onFocus = () => {
// Open date picker
}
onBlur = () => {
// Close date picker and add value to textinput
}
<TouchableOpacity
onPress={() => {
alert('hello');
}}>
<View pointerEvents="none">
<TextInput
onChange={() => {}}
value={''}
placeholder={waterMark}
editable={!isDisabled}
selectTextOnFocus={!isDisabled}
/>
</View>
</TouchableOpacity>