[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,  
}  
})
相关推荐
狂炫冰美式8 分钟前
人均配了AI, 为什么公司还是没变快? 🤔 本质还是分布式系统问题
前端·后端·架构
乘风gg1 小时前
多 Agent 不是万能的!搞懂这 5 个原则,少走 1 年弯路!
前端·agent·ai编程
猩猩程序员2 小时前
Vercel 推出 Agent 框架 Eve:让 AI Agent 像写 Web 应用一样简单
前端
爱读源码的大都督2 小时前
Claude Code源码分析(三):为什么系统提示词中需要有tools呢?
前端·人工智能·后端
爱勇宝2 小时前
Claude Code 被曝暗藏“隐形检测”代码:封代理不是最可怕的,可怕的是你根本不知道它在干什么
前端·后端·程序员
小牛不牛的程序员2 小时前
我用 Claude Code 半天撸完了一个完整网站,AI 编程到底提升了多少效率?
前端
东风破_2 小时前
JavaScript 面试常考的字符串算法:从反转字符串到回文判断
前端·javascript
ITOM运维行者3 小时前
从零搭建企业级服务器监控体系:踩坑实录与架构设计
前端·后端
monologues3 小时前
深入 Vue 3 源码:响应式系统的精妙设计与编译优化
前端
hunterandroid3 小时前
Paging 3 分页:从手动分页到声明式加载
前端