如何在 React Native 中对 TextInput 应用单击事件 [英] How to apply click event on TextInput in react native

查看:47
本文介绍了如何在 React Native 中对 TextInput 应用单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的本机应用程序中使用 TextInput.我想在点击 TextInput 时打开日期对话框.我没有找到任何解决方法来在 TextInput 上应用点击事件.有谁知道如何在 react native 中做到这一点?

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)}
/>

推荐答案

您可以使用 onFocus 属性来了解用户何时选择了该 TextInput.

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()}
/>

除非你有 autofocus={true},否则这会起作用.您可以在此处找到有关 onFocus 的更多文档:

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

这篇关于如何在 React Native 中对 TextInput 应用单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆