How can I put an icon inside a TextInput in React Native?

前端 未结 9 2107
轻奢々
轻奢々 2020-12-04 19:03

I am thinking of having something like this https://android-arsenal.com/details/1/3941 where you have icon that you press to show password as plaintext, not as dots. However

9条回答
  •  北海茫月
    2020-12-04 19:33

    Here you have an example I took from my own project, i have just removed what i thought we didnt need for the example.

    import React, { Component } from 'react';
    import {
      Text,
      TouchableOpacity,
      View,
      StyleSheet,
      Dimensions,
      Image
    } from 'react-native';
    
    class YourComponent extends Component {
      constructor(props) {
        super(props);
    
        this._makeYourEffectHere = this._makeYourEffectHere.bind(this);
    
        this.state = {
            showPassword: false,
            image: '../statics/showingPassImage.png'
        }
      }
    
      render() {
        return (
          
            
              button
              
            
            
          
        );
      }
    
      _makeYourEffectHere() {
        var png = this.state.showPassword ? '../statics/showingPassImage.png' : '../statics/hidingPassImage.png';
        this.setState({showPassword: !this.state.showPassword, image: png});
      }
    }
    
    var styles = StyleSheet.create({
      container: {
        flex: 1,
        backgroundColor: 'white',
        justifyContent: 'center',
        flexDirection: 'column',
        alignItems: 'center',
      },
      button: {
        width: Dimensions.get('window').width * 0.94,
        height: 40,
        backgroundColor: '#3b5998',
        marginTop: Dimensions.get('window').width * 0.03,
        justifyContent: 'center',
        borderRadius: Dimensions.get('window').width * 0.012
      },
      image: {
        width: 25,
        height: 25,
        position: 'absolute',
        left: 7,
        bottom: 7
      },
      input: {
        width: Dimensions.get('window').width * 0.94,
        height: 30
      }
    });
    
    module.exports = YourComponent;
    

    I hope It helps you.

    Let me know if it was useful.

提交回复
热议问题