React Native之TextInput的介紹與使用(富文本封裝與使用實例,常用輸入框封裝與使用實例)


React Native之TextInput的介紹與使用(富文本封裝與使用實例,常用輸入框封裝與使用實例)

TextInput組件介紹

TextInput是一個允許用戶在應用中通過鍵盤輸入文本的基本組件。本組件的屬性提供了多種特性的配置,譬如自動完成、自動大小寫、占位文字,以及多種不同的鍵盤類型(如純數字鍵盤)等等。最簡單的用法就是丟一個TextInput到應用里,然后訂閱它的onChangeText事件來讀取用戶的輸入。

組件的常用屬性

(1)autoCapitalize:控制TextInput是否要自動將特定字符切換為大寫。可選值有:none、sentences、words、characters。
  • characters: 所有的字符。
  • words: 每個單詞的第一個字符。
  • sentences: 每句話的第一個字符(默認)。
  • none: 不自動切換任何字符為大寫。
(2)placeholder:占位符,在輸入前顯示的文本內容。
(3)value:文本輸入框的默認值。
(4)placeholderTextColor:占位符文本的顏色。
(5)secureTextEntry:如果為 true,表示密碼輸入框。文本顯示為“*”,默認值為false。
(6)multiline:如果為 true,表示多行輸入。
(7)editable:默認為 true。如果設置為 false 表示不可編輯。
(8)autoFocus:如果為 true,則自動獲取焦點。
(9)clearButtonMode:表示什么時候會顯示清除按鈕。可選值有:never、while-editing、unless-editing、always。
(10)maxLength:能夠輸入的最長字符數。
(11)enablesReturnKeyAutomatically:默認為 false。設置為 true 表示沒有輸入文本時返回鍵無法使用。
(12)returnKeyType:表示軟鍵盤返回鍵顯示的字符串。可選值為:default、go、google、join、next、route、search、send、yahoo、done、emergency-call。
(13)secureTextEntry:默認為 false。如果為 true,則像密碼框一樣隱藏輸入內容。
(14)autoCorrect:如果為false,會關閉拼寫自動修正。默認值是true。
(15)blurOnSubmit:如果為true,文本框會在提交的時候失焦。對於單行輸入框默認值為true,多行則為false。注意:對於多行輸入框來說,如果將blurOnSubmit設為true,則在按下回車鍵時就會失去焦點同時觸發onSubmitEditing事件,而不會換行。
(16)caretHidden:如果為true,則隱藏光標。默認值為false。
(17)keyboardType:keyboardType  enum("default", 'numeric', 'email-address', "ascii-capable", 'numbers-and-punctuation', 'url', 'number-pad', 'phone-pad', 'name-phone-pad', 'decimal-pad', 'twitter', 'web-search') ,決定彈出的何種軟鍵盤的,譬如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來自定義文本。

跨平台

下列這些選項是跨平台可用的:

  • done
  • go
  • next
  • search
  • send

限Android

下列這些選項僅限Android使用:

  • none
  • previous

限iOS

下列這些選項僅限iOS使用:

  • default
  • emergency-call
  • google
  • join
  • route
  • yahoo

(19)selectTextOnFocus:如果為true,當獲得焦點的時候,所有的文字都會被選中。

(20)selection:selection {start: number, end: number} 設置選中文字的范圍(指定首尾的索引值)。如果首尾為同一索引位置,則相當於指定光標的位置。

(21)selectionColor:設置輸入框高亮時的顏色(在iOS上還包括光標)

 組件的常用方法

(1)onChange:當文本發生變化時,調用該函數。
(2)onEndEditing:當結束編輯時,調用該函數。
(3)onBlur:失去焦點時觸發。
(4)onFocus:獲得焦點時觸發。
(5)onSubmitEditing:當結束編輯后,點擊鍵盤的提交按鈕觸發該事件。
(6)onChangeText:當文本框內容變化時調用此回調函數。改變后的文字內容會作為參數傳遞。
(7)onLayout:當組件掛載或者布局變化的時候調用,參數為 {x, y, width, height}
(8)onScroll:在內容滾動時持續調用,傳回參數的格式形如 { nativeEvent: { contentOffset: { x, y } } }。 也可能包含其他和滾動事件相關的參數,但是在Android上,出於性能考慮,不會提供contentSize參數。
(9)onSelectionChange:長按選擇文本時,選擇范圍變化時調用此函數,傳回參數的格式形如  { nativeEvent: { selection: { start, end } } }
(10)iosonKeyPress:當一個鍵被按下的時候調用此回調。傳遞給回調函數的參數為 { 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 });


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM