簡體   English   中英

如何在 React Native 中對 TextInput 應用單擊事件

[英]How to apply click event on TextInput in react native

嗨,我在我的本機應用程序中使用TextInput 我想在單擊TextInput打開日期對話框。 我沒有找到任何解決方法來在TextInput上應用點擊事件。 有誰知道如何在 react native 中做到這一點?

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

TextInput添加pointerEvents="none"

示例

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

執行 TextInput 單擊的另一種方法

TextInput onTouchStart

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

您可以使用onFocus道具了解用戶何時選擇了該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更多文檔:

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

使用onTouchStart屬性在單擊事件上調用任何函數

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM