reactReact Native01-混合开发

React Native实战系列第八篇 — TextInput组

2017-05-22  本文已影响488人  撩课_叶建华

一、前言

<p></p>
<p></p>

二、TextInput的常见属性

因为TextInput是继承自UIView,所以View的属性TextInput也能够使用,一些样式类的属性在学习的时候可以参照View的相关属性。

<p></p>

监听输入的做法
代码实操
render() {
        return (
            <View style={styles.container}>
                <TextInput
                    underlineColorAndroid='transparent'
                    style={styles.textInputStyle}
                    // value="我是默认文字"
                    // keyboardType="email-address"
                    // keyboardAppearance="dark"
                    // returnKeyType="go"
                    // multiline={false}
                    // password={true}
                    placeholder = '我是占位文字'
                    clearButtonMode="always"
                />
            </View>
        );

<p></p>
<p></p>
<p></p>
<p></p>

三、Demo综合演练 --- 简单的登录界面

import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Image,
    TextInput,
    TouchableOpacity
} from 'react-native';

// 获取当前设备的宽度
const Dimensions = require('Dimensions');
const {width, height} = Dimensions.get('window');


export default class XZHQQLogin extends Component {
    render() {
        return (
            <View style={styles.container}>
               {/*logo*/}
               <Image source={require('./img/icon.png')} style={styles.iconStyle}/>
               {/*输入框*/}
               <TextInput
                   placeholder="请输入用户名"
                   clearButtonMode="always"
                   underlineColorAndroid="transparent"
                   style={styles.inputViewStyle}
               />
               <TextInput
                   placeholder="请输入密码"
                   password={true}
                   clearButtonMode="always"
                   underlineColorAndroid="transparent"
                   style={styles.inputViewStyle}
               />
               {/*登录按钮*/}
               <TouchableOpacity
                   activeOpacity={0.5}
                   // 按事件(按下和抬起)
                   onPress={()=>this._login()}
                   style={styles.loginBtnStyle}
               >
                   <Text style={{color:'#fff',fontSize:18}}>登  录</Text>
               </TouchableOpacity>
               {/*默认设置*/}
               <View style={styles.defaultSettingStyle}>
                   {/*左边*/}
                   <TouchableOpacity
                       onLongPress={()=>{alert('长按事件!!!');}}
                   >
                       <Text>无法登录</Text>
                   </TouchableOpacity>
                   {/*右边*/}
                   <TouchableOpacity>
                       <Text>新用户</Text>
                   </TouchableOpacity>
               </View>
                {/*默认设置*/}
                <View style={styles.bottomViewStyle}>
                   <Text>其它方式登录:</Text>
                   <Image source={require('./img/icon3.png')} style={styles.bottomImgStyle}/>
                   <Image source={require('./img/icon7.png')} style={styles.bottomImgStyle}/>
                   <Image source={require('./img/icon8.png')} style={styles.bottomImgStyle}/>
                </View>
            </View>
        );
    }

    /**
     * 处理登录
     * @private
     */
    _login(){
        alert('请输入用户名');
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        backgroundColor: '#e8e8e8',
    },

    iconStyle:{
        width:80,
        height:80,
        borderRadius: 40,
        marginTop: 80,
        marginBottom:30,
        borderWidth:2,
        borderColor:'#fff'
    },

    inputViewStyle:{
        width:width,
        height:36,
        backgroundColor:'#fff',
        marginBottom:1,
        textAlign:'center'
    },

    loginBtnStyle:{
        height:36,
        width:width * 0.95,
        backgroundColor:'#e9232c',
        marginTop:30,
        marginBottom:20,
        borderRadius:5,

        /*主侧轴居中*/
        justifyContent:'center',
        alignItems:'center',
    },

    defaultSettingStyle:{
        width:width * 0.95,

        /*改变主轴的方向*/
        flexDirection:'row',
        justifyContent:'space-between'
    },

    bottomViewStyle:{
        position:'absolute',
        left:10,
        bottom:10,

        /*改变主轴的方向*/
        flexDirection:'row',
        alignItems:'center'
    },

    bottomImgStyle:{
        width:40,
        height:40,
        borderRadius:20,
        marginLeft:10
    }
});

module.exports = XZHQQLogin;

<p></p>
<p></p>

案例运行结果

<p></p>
<p></p>
<p></p>
<p></p>

长按图片-->识别图中二维码

近期正在把公众账号的文章转移到简书,如果要了解第一动态,请关注我的微信公众账号,带你从零到一的进行React Native开发实战,在其他文章中会有对应的code和资料发放!

上一篇 下一篇

猜你喜欢

热点阅读