繁体   English   中英

React Native没有从TextInput显示Datepicker

[英]React Native doesn't show Datepicker from TextInput

我想知道是否可以在按下TextInput时显示datepicker模式。

我正在使用TouchableWithoutFeedbackTextInput 如果我使用Text ,它会很好地工作,但是当我使用TextInput时,不会显示datepicker模式。

我使用TextInput的原因是因为我想使用underlineColorAndroid prop。

这是代码

showPicker = async (stateKey, options) => {
    try {
      const newState = {};
      const { action, year, month, day } = await DatePickerAndroid.open(options);
      if (action === DatePickerAndroid.dismissedAction) {
        newState[stateKey + 'Text'] = 'dismissed';
      } else {
        const date = new Date(year, month, day);
        newState[stateKey + 'Text'] = date.toLocaleDateString();
        newState[stateKey + 'Date'] = date;
      }
      this.setState(newState);
    } catch ({ code, message }) {
      console.warn(`Error in example '${stateKey}': `, message);
    }
  };

<TouchableWithoutFeedback
            onPress={this.showPicker.bind(this, 'simple', { date: this.state.simpleDate })}
          >
            <TextInput
              placeholder="Where the event held*"
              onFocus={() => this.onFocus()}
              onFocus={() => this.onFocus()}
              style={{ 
                height: 60, backgroundColor: this.state.backgroundColor, color: this.state.color 
              }}
              value={this.state.simpleText}
            />
          </TouchableWithoutFeedback>

目的是每当我按下/单击TextInput ,它将弹出日期选择器模式,然后我可以从模式中选择日期。

您只需要在TextInput的onFocus道具中调用showPicker函数即可。 像那样:

<TextInput onFocus = {this.showPicker.bind(this, 'simple', { date: this.state.simpleDate })}>

如果我的想法与您的目标不符,请原谅我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM