[react-native] 08 Image + TextInput 组件(登录注册铺垫)

Image 组件

作用:加载图片

加载方式:

  • 本地路径
  • 图片的URL地址
  • 图片的Base64字符串

导入 组件

import {Text, View, ScrollView, StyleSheet, Image} from 'react-native'

js 复制代码
//image组件
<Image  
    style={[styles.itemImg]}  
    source={require('../image/demo_pic.jpeg')}  
/>
js 复制代码
//添加样式
const styles = StyleSheet.create({  
    container:{  
        flex:1,  
        justifyContent:'center',  
        alignItems:'center'  
    },  

    itemImg:{  
        height:200,  
        width:Dimensions.get('window').width,  
        marginVertical:20  
    }  
})

TextInput 组件

导入组件

import {Text, View, ScrollView, StyleSheet, TextInput, Dimensions} from 'react-native'

调用组件

js 复制代码
// 调用组件
<View style={[styles.container]}>  
    <TextInput  
    style={[styles.input]}  
    />  
</View>
js 复制代码
//添加样式
container:{  
    flex:1,  
    justifyContent:'center',  
    alignItems:'center'  
},  
  
input:{  
    width:Dimensions.get('window').width,  
    margin:10,  
    borderWidth:1,  
    borderColor:'red',  
    paddingHorizontal:5  
}

添加 placeholder 和 按钮

js 复制代码
<TextInput  
    style={[styles.input]}  
    placeholder="请输入用户名"  
/>  
  
<View style={[styles.btn]}>  
    <Button title="登录" onPress={this.doLogin}/>  
</View>

添加构造函数 和 doLogin 函数

js 复制代码
constructor(){  
    super()  

    this.state = {username:''}  
}  
  
  
doLogin = () => {alert(this.state.username)}

添加 btn 样式

js 复制代码
btn:{  
    margin:10,  
    width:Dimensions.get('window').width -20,  
}

效果图如下:

获取用户名

js 复制代码
<TextInput  
    style={[styles.input]}  
    placeholder="请输入用户名"  
    value={this.state.username}  
    onChangeText={( val ) => {  
        this.setState({  
        username : val  
    })}}  
/>

获取密码

添加密码进 state

js 复制代码
constructor(){  
    super()  

    this.state = {username:'', password:''}  
}

添加一个新的 TextInput 组件

js 复制代码
<TextInput  
    style={[styles.input]}  
    placeholder="请输入密码"  
    value={this.state.password}  
    secureTextEntry={true}  
    onChangeText={  
        (val) => {  
            this.setState({ password:val})  
        }  
    }  
/>

不显示输入文本 --->secureTextEntry={true}

效果如图

默认键盘格式

js 复制代码
<TextInput  
    style={[styles.input]}  
    placeholder="请输入你的手机号"  
    keyboardType="number-pad"  
/>

这里需要注意keyboardType="number-pad" 的用法

js 复制代码
//文本域
<TextInput  
    style={[styles.input]}  
    placeholder="请输入自我介绍(5行)"  
    multiline={true}  
    numberOfLines={5}  
    textAlignVertical="top"
/>

全部代码

js 复制代码
import React, {Component} from 'react'  
import {Text, View, ScrollView, StyleSheet, TextInput, Dimensions, Button} from 'react-native'  
  
export default class FlexDirection extends Component{  
  
constructor(){  
super()  
  
this.state = {username:'', password:''}  
}  
  
  
doLogin = () => {alert(this.state.username + ' ' + this.state.password)}  
render(){  
return (  
<View style={[styles.container]}>  
<TextInput  
style={[styles.input]}  
placeholder="请输入用户名"  
value={this.state.username}  
onChangeText={( val ) => {  
this.setState({  
username : val  
})}}  
/>  
  
  
<TextInput  
style={[styles.input]}  
placeholder="请输入密码"  
value={this.state.password}  
secureTextEntry={true}  
onChangeText={  
(val) => {  
this.setState({ password:val})  
}  
}  
/>  
  
  
  
<TextInput  
style={[styles.input]}  
placeholder="请输入你的手机号"  
keyboardType="number-pad"  
/>  
  
  
<TextInput  
style={[styles.input]}  
placeholder="请输入自我介绍(5行)"  
multiline={true}  
numberOfLines={5}  
textAlignVertical="top"  
/>  
  
<View style={[styles.btn]}>  
<Button title="登录" onPress={this.doLogin}/>  
</View>  
  
  
  
</View>  
)  
}  
}  
  
const styles = StyleSheet.create({  
container:{  
flex:1,  
justifyContent:'center',  
alignItems:'center'  
},  
  
input:{  
width:Dimensions.get('window').width -20,  
margin:10,  
borderWidth:1,  
borderColor:'red',  
paddingHorizontal:5  
},  
  
btn:{  
margin:10,  
width:Dimensions.get('window').width -20,  
}  
})
相关推荐
lyj16899711 分钟前
vue-i18n+vscode+vue 多语言使用
前端·vue.js·vscode
小白变怪兽2 小时前
一、react18+项目初始化(vite)
前端·react.js
ai小鬼头2 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
墨菲安全3 小时前
NPM组件 betsson 等窃取主机敏感信息
前端·npm·node.js·软件供应链安全·主机信息窃取·npm组件投毒
GISer_Jing3 小时前
Monorepo+Pnpm+Turborepo
前端·javascript·ecmascript
天涯学馆3 小时前
前端开发也能用 WebAssembly?这些场景超实用!
前端·javascript·面试
我在北京coding4 小时前
TypeError: Cannot read properties of undefined (reading ‘queryComponents‘)
前端·javascript·vue.js
前端开发与ui设计的老司机4 小时前
UI前端与数字孪生结合实践探索:智慧物流的货物追踪与配送优化
前端·ui
全能打工人4 小时前
前端查询条件加密传输方案(SM2加解密)
前端·sm2前端加密
翻滚吧键盘5 小时前
vue绑定一个返回对象的计算属性
前端·javascript·vue.js