[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,  
}  
})
相关推荐
樊小肆10 小时前
# 你还在等DeepSeek官方 agent Harness‌? 来试试 DeepSeeker-Code吧
前端·人工智能·后端
樊小肆10 小时前
2568 万 token 才花 2 块 2:聊聊 DeepSeeker-Code 怎么吃满上下文缓存
前端·人工智能·后端
JarvanMo10 小时前
Flutter 3.47: material/cupertino终于解耦了
前端
郭邯10 小时前
一次「字符洁癖」引发的思考:我用 AI 写了个全角半角转换工具
前端
想要成为糕糕手10 小时前
🚀 在浏览器里跑 DeepSeek-R1?WebGPU 端侧推理实战(五)—— 中断、重置、缓存与流式生成
前端·react.js·llm
GuWenyue10 小时前
后端接口又双叒没写好?3 步搭建前端 Mock,从此告别"傻等后端"
前端·mocha
海兰10 小时前
【数据采集】开源的 Web 数据采集与处理Firecrawl(一)
前端·开源
vivo互联网技术10 小时前
从一键检测到 AI 修复:我们如何把无障碍检查做进研发流程
前端·人工智能
liuxiaocheng10 小时前
聊聊 Vercel AI SDK 的流式协议:前后端到底是怎么"边想边说"的
前端·后端
胡萝卜术10 小时前
在浏览器中跑 DeepSeek-R1:WebGPU 推理全流程深度解析
前端·javascript·面试