React-Native通過登入介面學習TextInput元件
阿新 • • 發佈:2019-02-02
同時適配Android和IOS
程式碼註釋比較詳細
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TextInput,
Platform,
TouchableOpacity,
} from 'react-native';
//獲取螢幕資訊
var Dimensions = require('Dimensions');
var width = Dimensions.get('window').width;
class TextInputG extends Component {
render() {
return (
<View style={styles.container}>
{/*賬號輸入框在這裡用View包裹以便處理器樣式*/}
<View style={styles.textInputViewStyle}>
<TextInput
style={styles.textInputStyle}
//站位符
placeholder='手機號'/>
</View>
{/*密碼輸入框*/}
<View style={styles.textInputViewStyle}>
<TextInput
style={styles.textInputStyle}
placeholder='密碼'
//密文
secureTextEntry={true}/>
</View>
{/*設定控制元件可點選*/}
<TouchableOpacity onPress={()=>{alert('點選登入')}}>
{/*登入按鈕*/}
<View style={styles.textLoginViewStyle}>
<Text style={styles.textLoginStyle}>登入</Text>
</View>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
//設定佔滿螢幕
flex: 1,
// backgroundColor: 'black',
marginTop: 140,
},
//包裹輸入框View樣式
textInputViewStyle: {
//設定寬度減去30將其居中左右便有15的距離
width: width - 30,
height: 45,
//設定圓角程度
borderRadius: 18,
//設定邊框的顏色
borderColor: 'blue',
//設定邊框的寬度
borderWidth: 1,
//內邊距
paddingLeft: 10,
paddingRight: 10,
//外邊距
marginTop: 10,
marginLeft: 20,
marginRight: 20,
//設定相對父控制元件居中
alignSelf: 'center',
},
//輸入框樣式
textInputStyle: {
width: width - 30,
height: 35,
paddingLeft: 8,
backgroundColor: '#00000000',
// alignSelf: 'center',
//根據不同平臺進行適配
marginTop: Platform.OS === 'ios' ? 4 : 8,
},
//登入按鈕View樣式
textLoginViewStyle: {
width: width - 30,
height: 45,
backgroundColor: 'red',
borderRadius: 20,
marginTop: 30,
alignSelf: 'center',
justifyContent: 'center',
alignItems: 'center',
},
//登入Text文字樣式
textLoginStyle: {
fontSize: 18,
color: 'white',
},
});
module.exports = TextInputG;