How to apply click event on TextInput in react native

我与影子孤独终老i 提交于 2020-01-15 05:12:29

问题


Hi I am using TextInput in my react native application. I want to open Date Dialog on clicking on TextInput. I didn't find any work around to apply click event on TextInput. Does anyone know how to do this in react native ?

<TextInput
  style={{
    height: 40,
    borderColor: "gray",
    borderWidth: 1,
    marginTop: 8
  }}
  underlineColorAndroid="transparent"
  placeholder={strings.schedule_date}
  onKeyPress={keyPress => console.log(keyPress)}
/>

回答1:


You can use the onFocus prop to know when the user has selected that TextInput.

<TextInput
  style={{
    height: 40,
    borderColor: "gray",
    borderWidth: 1,
    marginTop: 8
  }}
  underlineColorAndroid="transparent"
  placeholder={strings.schedule_date}
  onKeyPress={keyPress => console.log(keyPress)}
  onFocus={() => yourCode()}
/>

Unless you have autofocus={true}, this will work. You can find more documentation about onFocus here:

https://facebook.github.io/react-native/docs/textinput.html#onfocus




回答2:


Add pointerEvents="none" on TextInput

Example

<TouchableOpacity onPress={() => console.log("Pressed")}>
  <TextInput
   pointerEvents="none"
  />
</TouchableOpacity>



回答3:


One more way to perform TextInput click

TextInput onTouchStart

<TextInput
  style={{
    height: 40,
    borderColor: "gray",
    borderWidth: 1,
    marginTop: 8
  }}
  underlineColorAndroid="transparent"
  placeholder={strings.schedule_date}
  onTouchStart={()=>  console.log("Pressed...")}
  editable={false}
/>


来源:https://stackoverflow.com/questions/45781513/how-to-apply-click-event-on-textinput-in-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!