菜鸟教程小白 发表于 2022-12-12 12:54:57

javascript - React Native 不显示来自 TextInput 的日期选择器


                                            <p><p>我想知道是否可以在按下 TextInput 时显示日期选择器模式。</p>

<p>我正在使用 <code>TouchableWithoutFeedback</code> 和 <code>TextInput</code>。如果我使用 <code>Text</code> 效果很好,但是当我使用 TextInput 时,日期选择器模式不会显示。</p>

<p>我使用 TextInput 的原因是因为我想使用 <code>underlineColorAndroid</code> 属性。</p>

<p>这是代码</p>

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

&lt;TouchableWithoutFeedback
            onPress={this.showPicker.bind(this, &#39;simple&#39;, { date: this.state.simpleDate })}
          &gt;
            &lt;TextInput
            placeholder=&#34;Where the event held*&#34;
            onFocus={() =&gt; this.onFocus()}
            onFocus={() =&gt; this.onFocus()}
            style={{
                height: 60, backgroundColor: this.state.backgroundColor, color: this.state.color
            }}
            value={this.state.simpleText}
            /&gt;
          &lt;/TouchableWithoutFeedback&gt;
</code></pre>

<p><strong>目标</strong> 是每当我按下/单击 <code>TextInput</code> 时,它都会弹出日期选择器模态,然后我可以从模态中选择日期。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你只需要在 TextInput 的 <code>onFocus</code> 属性中调用 <code>showPicker</code> 函数。像那样:</p>

<pre><code>&lt;TextInput onFocus = {this.showPicker.bind(this, &#39;simple&#39;, { date: this.state.simpleDate })}&gt;
</code></pre>

<p>如果我的想法不符合您的目标,请原谅我。</p></p>
                                   
                                                <p style="font-size: 20px;">关于javascript - React Native 不显示来自 TextInput 的日期选择器,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42130921/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42130921/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: javascript - React Native 不显示来自 TextInput 的日期选择器