React Native之TextInput的介紹與使用(富文本封裝與使用實例,常用輸入框封裝與使用實例)
TextInput組件介紹
TextInput是一個允許用戶在應用中通過鍵盤輸入文本的基本組件。本組件的屬性提供了多種特性的配置,譬如自動完成、自動大小寫、占位文字,以及多種不同的鍵盤類型(如純數字鍵盤)等等。最簡單的用法就是丟一個TextInput到應用里,然后訂閱它的onChangeText事件來讀取用戶的輸入。
組件的常用屬性
- characters: 所有的字符。
- words: 每個單詞的第一個字符。
- sentences: 每句話的第一個字符(默認)。
- none: 不自動切換任何字符為大寫。
numeric(純數字鍵盤)。
這些值在所有平台都可用:
- default
- numeric
- email-address
- phone-pad
(18)iosreturnKeyType:iosreturnKeyType enum('done', 'go', 'next', 'search', 'send', 'none', 'previous', 'default', 'emergency-call', 'google', 'join', 'route', 'yahoo') 決定“確定”按鈕顯示的內容。在Android上你還可以使用returnKeyLabel來自定義文本。
跨平台
下列這些選項是跨平台可用的:
donegonextsearchsend
限Android
下列這些選項僅限Android使用:
noneprevious
限iOS
下列這些選項僅限iOS使用:
defaultemergency-callgooglejoinrouteyahoo
(19)selectTextOnFocus:如果為true,當獲得焦點的時候,所有的文字都會被選中。
(20)selection:selection {start: number, end: number} 設置選中文字的范圍(指定首尾的索引值)。如果首尾為同一索引位置,則相當於指定光標的位置。
(21)selectionColor:設置輸入框高亮時的顏色(在iOS上還包括光標)
組件的常用方法
{x, y, width, height}。
{ nativeEvent: { contentOffset: { x, y } } }。 也可能包含其他和滾動事件相關的參數,但是在Android上,出於性能考慮,不會提供contentSize參數。
{ nativeEvent: { selection: { start, end } } }。
{ nativeEvent: { key: keyValue } },其中keyValue即為被按下的鍵。會在onChange之前調用。
組件的使用實例
1,文本加輸入框(封裝組件 iOS Android)

封裝組件InputView.js的使用實例,如有需要完整的代碼,請留言評論
1 <View > 2 <Text>請認真填寫資料</Text> 3 <View style={{ marginTop: 12 }}> 4 <InputView name={'賬號'} 5 hintText={''} 6 editableV={false} 7 value={String(this.state.telephone)} 8 /> 9 <InputView name={'支付密碼'} 10 isPassword={true} 11 hintText={'請輸入數字+字母的組合'} 12 onChangeText={(inputData) => { this.setState({ password: inputData }) }} 13 /> 14 <InputView name={'再次確認'} 15 isPassword={true} 16 //value={this.state.nickname} 17 hintText={'請再次輸入'} 18 onChangeText={(inputData) => { this.setState({ confirmPass: inputData }) }} 19 /> 20 21 </View> 22 </View>
2,富文本編輯(封裝組件 iOS Android)

富文本RichTextView的使用實例,如有需要完整的代碼,請留言評論
1 <View style={{ marginTop: 20 }}> 2 <Text 3 style={{ 4 fontSize: 14, 5 color: AppSetting.BLACK, 6 paddingLeft: 20, 7 marginBottom: 10 8 }}>獎品描述</Text> 9 <RichTextView 10 inputStyle={styles.inputStyle} 11 placeholder="請填寫獎品描述(1000字以內哦)" 12 minHeight={240} // 最小高度,默認為100 13 maxLength={1000} // 最大長度,默認為100 14 onChangeText={(inputValue) => { 15 let desPrizes = CommonMethod.filteremoji(inputValue, 1)//表情過濾機制 16 this.setState({ desPrizes: desPrizes }) 17 }} 18 showCount={true} // 展示剩余文字, 默認為true 19 /> 20 21 </View>
RichTextView.js富文本編輯組件
1 /** 2 * Created by jackson on 2018/08/13. 3 * 富文本 4 */ 5 import React, {Component} from 'react'; 6 import PropTypes from 'prop-types' 7 import { 8 StyleSheet, 9 View, 10 TextInput, 11 Text, 12 Dimensions 13 } from 'react-native'; 14 const ScreenHeight = Dimensions.get('window').height; 15 const ScreenWidth = Dimensions.get('window').width; 16 const defaultMinHeight = 100 17 //模塊聲名並導出 18 export default class RichTextView extends Component { 19 //屬性聲名 20 static propTypes = { 21 style:PropTypes.object, 22 inputStyle:PropTypes.any, 23 maxLength:PropTypes.number, // 限制文字長度 24 placeholder:PropTypes.string, // 占位文字 25 minHeight:PropTypes.number, // 最小高度 26 showCount:PropTypes.bool, 27 onChangeText:PropTypes.func,//獲取編輯的文本 28 }; 29 30 //默認屬性 31 static defaultProps = { 32 maxLength: 100, 33 showCount: true, 34 minHeight: defaultMinHeight 35 }; 36 37 //構造函數 38 constructor(props) { 39 super(props); 40 //狀態機變量聲明 41 this.state = { 42 text: '', 43 }; 44 } 45 46 //渲染 47 render() { 48 return ( 49 <View style={styles.container}> 50 <View style={[styles.inputViewStyle,this.props.style,{minHeight:this.props.minHeight}]}> 51 <TextInput 52 style={[styles.inputTextStyle,this.props.inputStyle,{minHeight:this.props.minHeight}]} 53 placeholder={this.props.placeholder ? this.props.placeholder :'請輸入'} 54 multiline={true} 55 paddingVertical={0} 56 selectionColor = {'#b2b2b2'} 57 textAlignVertical={'top'} 58 placeholderTextColor={'#b2b2b2'} 59 underlineColorAndroid={'transparent'} 60 maxLength={this.props.maxLength} 61 defaultValue = {this.state.text} 62 onChangeText={ 63 (text) => { 64 this.props.onChangeText(text) 65 this.setState({ 66 text: text 67 }) 68 } 69 } 70 /> 71 { 72 this.props.showCount ? 73 <Text style={{position: 'absolute', bottom: 5, right: 10, fontSize: 14}}> 74 {this.state.text.length}/{this.props.maxLength} 75 </Text> 76 : 77 null 78 } 79 </View> 80 </View> 81 ); 82 } 83 }; 84 85 const styles = StyleSheet.create({ 86 container: { 87 flex: 1, 88 alignItems: 'center', 89 backgroundColor: '#FFFFFF', 90 }, 91 inputViewStyle: { 92 width:ScreenWidth - 40, 93 minHeight: defaultMinHeight, 94 }, 95 inputTextStyle: { 96 fontSize: 14, 97 color: '#666666', 98 width: '100%', 99 minHeight: defaultMinHeight, 100 padding: 10, 101 paddingBottom: 30, 102 paddingTop: 10 103 } 104 });
