password.js 4.27 KB
/**
 * Created by DEV005 on 2017/10/27.
 */
import React, {Component, PropTypes} from 'react';
import {
    StyleSheet,
    Image,
    TouchableOpacity,
    TouchableHighlight,
    ScrollView,
    Button,
    Text,
    View,
    NativeModules,
    TextInput,
    DeviceEventEmitter
} from 'react-native';
import {xnToast} from "../../utils/utils";
import AppService from "../../service/AppService";

export default class password extends Component {

    constructor(props){
        super(props);
        this.state = {
            password:""
        };
    }

    componentDidMount() {

        let _this=this;
        DeviceEventEmitter.addListener('initPassword', function(event) {
            // handle event.
            _this.setState({
                password:"",
            });
           if(event=="blur"&&_this.refs.TextInput){
               _this.refs.TextInput.blur();
           }
        });

    };
//  获取焦距
    autoFocus=()=>{
        this.refs.TextInput.focus();
    };
    //改变
    onChangeText=(value)=>{
        let  _this=this;
        this.setState({
            password:value,
        });
        if(value.length==6){
            _this.props.callBack(value);
        }
    };
   
    render() {

        return (
            <View style={styles.loginLayout}>
                <View style={styles.titleLayout}><Text style={styles.titleText}>{this.props.title}</Text></View>
                <TouchableOpacity style={styles.loginInput} activeOpacity={1}  onPress={()=>{this.autoFocus()}}>
                    <View style={styles.inputItem}>{this.state.password.length>0&&<Image  style={styles.inputText} source={require('../../img/password.png')}  resizeMode="contain"/>}</View>
                    <View style={styles.inputItem}>{this.state.password.length>1&&<Image  style={styles.inputText} source={require('../../img/password.png')}  resizeMode="contain"/>}</View>
                    <View style={styles.inputItem}>{this.state.password.length>2&&<Image  style={styles.inputText} source={require('../../img/password.png')}  resizeMode="contain"/>}</View>
                    <View style={styles.inputItem}>{this.state.password.length>3&&<Image  style={styles.inputText} source={require('../../img/password.png')}  resizeMode="contain"/>}</View>
                    <View style={styles.inputItem}>{this.state.password.length>4&&<Image  style={styles.inputText} source={require('../../img/password.png')}  resizeMode="contain"/>}</View>
                    <View style={[styles.inputItem,styles.inputItemEnd]}>{this.state.password.length>5&&<Image  style={styles.inputText} source={require('../../img/password.png')}  resizeMode="contain"/>}</View>
                </TouchableOpacity>
                {this.props.toForget&&<View style={styles.linkLayout}><TouchableOpacity style={styles.link} onPress={()=>{this.props.toForget()}}><Text style={styles.linkText}>忘记密码?</Text></TouchableOpacity></View>}
                <TextInput   style={styles.hide}   autoFocus={false}  ref="TextInput" value={this.state.password}
                           onChangeText={(text) => {this.onChangeText(text)}}   keyboardType='numeric'    maxLength ={6} />
               
            </View>
        );
    };

};

const styles = StyleSheet.create({
    loginLayout:{
        flex:1,
        paddingTop:30,
        paddingLeft:25,
        paddingRight:25,
        backgroundColor:"#eee"
    },
    hide:{
        width:0,
        height:0
    },
    titleLayout:{
        height:30,
        marginBottom:10,
    },
    titleText:{
        fontSize:20,
        color:"#000",
    },
    loginInput:{
        flexDirection:"row",
        backgroundColor:"#fff",
        borderWidth:1,
        borderColor:"#ddd",
        borderRadius:4,
        paddingTop:10,
        paddingBottom:10,
        height:50,
    },
    inputItemEnd:{
        borderColor:"transparent",
    },
    inputItem:{
        flex:1,
        flexDirection:"row",
        alignItems: 'center',
        justifyContent: 'center',
        borderColor:"#ddd",
        borderRightWidth:1,
    },
    linkLayout:{
        paddingTop:10,
        flexDirection:"row",
        justifyContent:"flex-end",
    },
    inputText:{
        width:12,
        height:12,
    },
    link:{

    },
    linkText:{
        paddingTop:5,
        paddingBottom:5,
        color:"#39f",
    }

});