菜鸟教程小白 发表于 2022-12-11 20:26:25

ios - react native 中的TextInput显示具有颜色背景的文本值


                                            <p><p>我正在尝试使用半透明的文本输入在 nativereact 中制作登录屏幕。但是当我在输入中键入文本时出现了一种奇怪的行为:键入的文本看起来像是突出显示的(但不是)。正如您在此屏幕截图中看到的:</p>

<p>(好像上传到imgur失败,所以:<a href="https://image.ibb.co/hvBDQe/Image_1.jpg" rel="noreferrer noopener nofollow">https://image.ibb.co/hvBDQe/Image_1.jpg</a>)</p>

<p>这是我的代码:</p>

<p></p><div class="snippet"data-lang="js"data-hide="false"data-console="true"data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>class LoginForm extends Component {

    //#region Constructeurs   
    constructor(props) {
      // Appel du constructeur de Component
      super(props);
      // Initialise les propriétés du composant
      this.state = {
            isLoading: false,
            usernameInput: &#34;&#34;,
            passwordInput: &#34;&#34;,
            urlInput: &#34;&#34;,
            portInput: &#34;443&#34;
      };
    }
    //#endregion

    //#region Cycle de vie du Component
    componentDidMount() {
      
    }

    render() {

      return (
            &lt;View style={styles.mainContainer} pointerEvents={this.state.isLoading ? &#39;none&#39; : &#39;auto&#39;}&gt;
                &lt;TextInput style = {styles.input}
                            autoCapitalize=&#34;none&#34;
                            onSubmitEditing={() =&gt; this.passwordInput.focus()}
                            autoCorrect={false}
                            keyboardType=&#39;email-address&#39;
                            returnKeyType=&#34;next&#34;
                            placeholder=&#39;*Email*&#39;
                            placeholderTextColor={COLOR_GREY_300}
                            value={this.state.usernameInput}
                            onChangeText={text =&gt; this.setState({usernameInput: text})}/&gt;
                &lt;TextInput style = {styles.input}   
                            returnKeyType=&#34;go&#34;
                            ref={(input)=&gt; this.passwordInput = input}
                            onSubmitEditing={() =&gt; this.urlInput.focus()}
                            placeholder=&#39;*Mot de passe*&#39;
                            returnKeyType=&#34;next&#34;
                            placeholderTextColor={COLOR_GREY_300}
                            secureTextEntry
                            value={this.state.passwordInput}
                            onChangeText={text =&gt; this.setState({passwordInput: text})}/&gt;
                &lt;TextInput style = {styles.input}
                            autoCapitalize=&#34;none&#34;
                            onSubmitEditing={() =&gt; this.portInput.focus()}
                            ref={(input)=&gt; this.urlInput = input}
                            autoCorrect={false}
                            returnKeyType=&#34;next&#34;
                            placeholder=&#39;*adresse url*&#39;
                            placeholderTextColor={COLOR_GREY_300}
                            value={this.state.urlInput}
                            onChangeText={text =&gt; this.setState({urlInput: text})}/&gt;
                &lt;TextInput style = {styles.input}
                            autoCapitalize=&#34;none&#34;
                            onSubmitEditing={this._onSubmitLogin}
                            ref={(input)=&gt; this.portInput = input}
                            autoCorrect={false}
                            keyboardType=&#39;number-pad&#39;
                            returnKeyType=&#34;go&#34;
                            placeholder=&#39;*port*&#39;
                            placeholderTextColor={COLOR_GREY_300}
                            value={this.state.portInput}
                            onChangeText={text =&gt; this.setState({portInput: text})} /&gt;
                &lt;TouchableOpacity style={styles.buttonContainer} onPress={this._onSubmitLogin}&gt;
                  &lt;Text style={styles.buttonText}&gt;*LOGIN*&lt;/Text&gt;
                &lt;/TouchableOpacity&gt;
                &lt;ActivityIndicator size=&#34;large&#34; color={COLOR_SECONDARY} animating={this.state.isLoading} style={styles.activityIndicator} /&gt;
            &lt;/View&gt;
      );
    }
    //#endregion

    //#region Listener
    _onSubmitLogin = () =&gt; {
      // Affichage du loader
      this.setState({
            isLoading: true
      });

      // Récupération de l&#39;API KEY
      let authController = new OBAuthController();
      authController.callPostCreateApiKey().then((apiKey) =&gt; {
            
            console.log(&#34;apiKey = &#34; + JSON.stringify(apiKey));
            // Masquage du loader
            this.setState({
                isLoading: false
            });
      });
    }
    //#endregion
}

export default LoginForm;

//#region Définition de la StyleSheet   
const styles = StyleSheet.create({
    mainContainer: {
      padding: 50
    },
    input:{
      height: 40,
      backgroundColor: &#39;rgba(225,225,225,0.3)&#39;,
      marginBottom: 16,
      padding: 10,
      color: &#39;#fff&#39;
    },
    buttonContainer:{
      backgroundColor: &#39;#2980b6&#39;,
      paddingVertical: 15
    },
    buttonText:{
      color: &#39;white&#39;,
      textAlign: &#39;center&#39;,
      fontWeight: &#39;700&#39;
    },
    activityIndicator: {
      position:&#39;absolute&#39;,
      alignSelf:&#39;center&#39;
    }
})
//#endregion</code></pre>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在@Hariks 评论之后,我最终将我的每个 TextInput 包装到一个 View 中:</p>

<p></p><div class="snippet"data-lang="js"data-hide="false"data-console="true"data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>&lt;View style={styles.inputView}&gt;
                  &lt;TextInput style = {styles.input}
                              autoCapitalize=&#34;none&#34;
                              onSubmitEditing={() =&gt; this.passwordInput.focus()}
                              autoCorrect={false}
                              keyboardType=&#39;email-address&#39;
                              returnKeyType=&#34;next&#34;
                              placeholder=&#39;*Email*&#39;
                              placeholderTextColor={COLOR_GREY_300}
                              value={this.state.usernameInput}
                              onChangeText={text =&gt; this.setState({usernameInput: text})}/&gt;
                &lt;/View&gt;
                &lt;View style={styles.inputView}&gt;
                  &lt;TextInput style = {styles.input}   
                              returnKeyType=&#34;go&#34;
                              ref={(input)=&gt; this.passwordInput = input}
                              onSubmitEditing={() =&gt; this.urlInput.focus()}
                              placeholder=&#39;*Mot de passe*&#39;
                              returnKeyType=&#34;next&#34;
                              placeholderTextColor={COLOR_GREY_300}
                              secureTextEntry
                              value={this.state.passwordInput}
                              onChangeText={text =&gt; this.setState({passwordInput: text})}/&gt;
                &lt;/View&gt;
                &lt;View style={styles.inputView}&gt;
                  &lt;TextInput style = {styles.input}
                              autoCapitalize=&#34;none&#34;
                              onSubmitEditing={() =&gt; this.portInput.focus()}
                              ref={(input)=&gt; this.urlInput = input}
                              autoCorrect={false}
                              returnKeyType=&#34;next&#34;
                              placeholder=&#39;*adresse url*&#39;
                              placeholderTextColor={COLOR_GREY_300}
                              value={this.state.urlInput}
                              onChangeText={text =&gt; this.setState({urlInput: text})}/&gt;

                &lt;/View&gt;
                &lt;View style={styles.inputView}&gt;
                  &lt;TextInput style = {styles.input}
                              autoCapitalize=&#34;none&#34;
                              onSubmitEditing={this._onSubmitLogin}
                              ref={(input)=&gt; this.portInput = input}
                              autoCorrect={false}
                              keyboardType=&#39;number-pad&#39;
                              returnKeyType=&#34;go&#34;
                              placeholder=&#39;*port*&#39;
                              placeholderTextColor={COLOR_GREY_300}
                              value={this.state.portInput}
                              onChangeText={text =&gt; this.setState({portInput: text})} /&gt;
                &lt;/View&gt;</code></pre>

                                                <p style="font-size: 20px;">关于ios -reactnative 中的TextInput显示具有颜色背景的文本值,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/51730122/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/51730122/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - react native 中的TextInput显示具有颜色背景的文本值