React-Native 之 TextInput使用

前言

  • 學(xué)習(xí)本系列內(nèi)容需要具備一定 HTML 開發(fā)基礎(chǔ),沒有基礎(chǔ)的朋友可以先轉(zhuǎn)至 HTML快速入門(一) 學(xué)習(xí)

  • 本人接觸 React Native 時(shí)間并不是特別長褂策,所以對其中的內(nèi)容和性質(zhì)了解可能會(huì)有所偏差质礼,在學(xué)習(xí)中如果有錯(cuò)會(huì)及時(shí)修改內(nèi)容,也歡迎萬能的朋友們批評指出,謝謝

  • 文章第一版出自簡書系奉,如果出現(xiàn)圖片或頁面顯示問題珊燎,煩請轉(zhuǎn)至 簡書 查看 也希望喜歡的朋友可以點(diǎn)贊惭嚣,謝謝

TextInput 文本輸入框

  • React Native中的文本輸入框使用和iOS比較相近,可能是因?yàn)?RN 首先封裝iOS端的緣故(這點(diǎn)對iOS開發(fā)者來說是個(gè)好消息)

  • TextInput也是繼承自 View,所以 View 的屬性 TextInput 也能使用悔政,一些樣式類的屬性可以參照 View 的相關(guān)屬性

  • 為了更好的講解 TextInput晚吞,先創(chuàng)建一個(gè)基本的文本輸入框

    // 視圖
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                    <TextInput style={styles.textInputStyle}></TextInput>
                </View>
            );
        }
    });
    
    // 樣式
    var styles = StyleSheet.create({
        container: {
            flex:1
        },

        textInputStyle: {
            // 設(shè)置尺寸
            width:width,
            height:40,
            marginTop:100,
            // 設(shè)置背景顏色
            backgroundColor:'green'
        }
    });


效果:

初始化效果.gif
  • Value:文本輸入的默認(rèn)值(注:如果設(shè)置了此屬性,會(huì)造成無法輸入的尷尬谋国,一般會(huì)搭配JS動(dòng)態(tài)設(shè)置)
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        value="設(shè)置了Value"
                    ></TextInput>
                </View>
            );
        }
    });

效果:

設(shè)置了Value.gif
  • keyboardType:設(shè)置鍵盤類型(決定使用哪種鍵盤)
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        keyboardType="number-pad"
                    ></TextInput>
                </View>
            );
        }
    });

效果:

設(shè)置鍵盤類型.gif
  • multiline:如果值為真槽地,文本輸入可以輸入多行,默認(rèn)值為假
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        multiline={true}
                    ></TextInput>
                </View>
            );
        }
    });

效果:

多行輸入.gif
  • password:如果值為真芦瘾,文本輸入框就成為一個(gè)密碼區(qū)域捌蚊,默認(rèn)值為假
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        password={true}
                    ></TextInput>
                </View>
            );
        }
    });

效果:

密碼模式.gif
  • placeholder:在文本輸入之前提示用戶文本框功能,也就是占位文字
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        placeholder="請輸入賬號"
                    ></TextInput>
                </View>
            );
        }
    });

效果:

占位文字.gif
  • placeholderTextColor:占位字符串的文本顏色
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        placeholder="請輸入賬號"
                        placeholderTextColor="red"
                    ></TextInput>
                </View>
            );
        }
    });

效果:

占位文字顏色.gif
  • autoCapitalize:控制TextInput是否要自動(dòng)將特定字符切換為大寫

    • none:不自動(dòng)使用任何東西
    • sentences:每個(gè)句子的首字母(默認(rèn))
    • words:每一個(gè)單詞的首字母
    • characters:所有字符
        var textInputTest = React.createClass({
            render(){
                return(
                    <View style={styles.container}>
                        {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        placeholder="none"
                        autoCapitalize="none"
                    ></TextInput>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        placeholder="sentences"
                        autoCapitalize="sentences"
                    ></TextInput>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        placeholder="words"
                        autoCapitalize="words"
                    ></TextInput>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        placeholder="characters"
                        autoCapitalize="characters"
                    ></TextInput>
                    </View>
                );
            }
        });
    
    

效果:


autoCapitalize.gif
  • autoCorrect:如果為false近弟,會(huì)關(guān)閉拼寫自動(dòng)修正缅糟。默認(rèn)值是true。
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    placeholder="沒有自動(dòng)改正拼寫"
                    autoCorrect={false}
                ></TextInput>
                {/* 文本輸入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    placeholder="自動(dòng)改正拼寫"
                    autoCorrect={true}
                ></TextInput>
                </View>
            );
        }
    });

效果:

autoCorrect.gif
  • autoFocus:如果為true祷愉,在componentDidMount后會(huì)獲得焦點(diǎn)窗宦。默認(rèn)值為false。
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        autoFocus={true}
                    ></TextInput>
                </View>
            );
        }
    });

效果:

autoFocus.gif
  • clearButtonMode:清除按鈕出現(xiàn)的時(shí)機(jī)

    • never:不出現(xiàn)
    • while-editing:編輯的時(shí)候出現(xiàn)
    • unless-editing:沒有編輯時(shí)出現(xiàn)
    • always:總是出現(xiàn)
        var textInputTest = React.createClass({
            render(){
                return(
                    <View style={styles.container}>
                        {/* 文本輸入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    placeholder="never"
                    clearButtonMode="never"
                ></TextInput>
                {/* 文本輸入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    placeholder="while-editing"
                    clearButtonMode="while-editing"
                ></TextInput>
                {/* 文本輸入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    placeholder="unless-editing"
                    clearButtonMode="unless-editing"
                ></TextInput>
                {/* 文本輸入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    placeholder="always"
                    clearButtonMode="always"
                ></TextInput>
                    </View>
                );
            }
        });
    
    

效果:

clearButtonMode.gif
  • clearTextOnFocus:如果為true二鳄,每次開始輸入的時(shí)候都會(huì)清除文本框的內(nèi)容

        var textInputTest = React.createClass({
            render(){
                return(
                    <View style={styles.container}>
                        {/* 文本輸入框 */}
                        <TextInput
                            style={styles.textInputStyle}
                            clearTextOnFocus={true}
                        ></TextInput>
                    </View>
                );
            }
        });
    
    

效果:

clearTextOnFocus.gif
  • editable:如果值為假赴涵,文本是不可編輯,默認(rèn)值為真
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        editable={false}
                    ></TextInput>
                </View>
            );
        }
    });

效果:

editable.gif
  • enablesReturnKeyAutomatically:如果為true订讼,鍵盤會(huì)在文本框內(nèi)沒有文字的時(shí)候禁用確認(rèn)按鈕髓窜。默認(rèn)值為false。
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    enablesReturnKeyAutomatically={true}
                ></TextInput>
                {/* 文本輸入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    enablesReturnKeyAutomatically={false}
                ></TextInput>
                </View>
            );
        }
    });

效果:

enablesReturnKeyAutomatically.gif
  • returnKeyType:決定返回鍵的樣式

    • default
    • go
    • google
    • join
    • next
    • route
    • search
    • send
    • yahoo
    • done
    • emergency-call


        var textInputTest = React.createClass({
            render(){
                return(
                    <View style={styles.container}>
                        {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        returnKeyType="go"
                    ></TextInput>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        returnKeyType="join"
                    ></TextInput>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        returnKeyType="done"
                    ></TextInput>
                    </View>
                );
            }
        });
    
    

效果:


returnKeyType.gif
  • secureTextEntry:如果值為真躯嫉,文本輸入框就會(huì)使輸入的文本變模糊纱烘,以便于像密碼這樣敏感的文本保持安全,類似 password 屬性祈餐,默認(rèn)值為假
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        keyboardType="number-pad"
                    ></TextInput>
                </View>
            );
        }
    });

效果:


secureTextEntry.gif
  • onChange:當(dāng)文本框內(nèi)容變化時(shí)調(diào)用此回調(diào)函數(shù)
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        onChange={() => {alert('文本框內(nèi)容改變')}}
                    ></TextInput>
                </View>
            );
        }
    });

效果:


onChange.gif
  • onChangeText:當(dāng)文本框內(nèi)容變化時(shí)調(diào)用此回調(diào)函數(shù)擂啥。改變后的文字內(nèi)容會(huì)作為參數(shù)傳遞
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        onChangeText={(Text) => {alert('文字改變')}}
                    ></TextInput>
                </View>
            );
        }
    });

效果:


onChangeText.gif
  • onFocus:當(dāng)文本框獲得焦點(diǎn)的時(shí)候調(diào)用此回調(diào)函數(shù)
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        onFocus={() => {alert('文本框獲得焦點(diǎn)')}}
                    ></TextInput>
                </View>
            );
        }
    });

效果:


onFoucs.gif
  • onBlur:當(dāng)文本框失去焦點(diǎn)的時(shí)候調(diào)用此回調(diào)函數(shù)
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        onBlur={() => {alert('失去焦點(diǎn)')}}
                    ></TextInput>
                </View>
            );
        }
    });

效果:


onBlur.gif
  • onEndEditing:結(jié)束編輯時(shí),調(diào)用回調(diào)函數(shù)
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本輸入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        onEndEditing={() => {alert('結(jié)束文本編輯')}}
                    ></TextInput>
                </View>
            );
        }
    });

效果:

onEndEditing.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末帆阳,一起剝皮案震驚了整個(gè)濱河市哺壶,隨后出現(xiàn)的幾起案子屋吨,更是在濱河造成了極大的恐慌,老刑警劉巖山宾,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件至扰,死亡現(xiàn)場離奇詭異,居然都是意外死亡资锰,警方通過查閱死者的電腦和手機(jī)敢课,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绷杜,“玉大人直秆,你說我怎么就攤上這事”廾耍” “怎么了圾结?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長齿诉。 經(jīng)常有香客問我筝野,道長,這世上最難降的妖魔是什么粤剧? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任歇竟,我火速辦了婚禮,結(jié)果婚禮上俊扳,老公的妹妹穿的比我還像新娘途蒋。我一直安慰自己猛遍,他們只是感情好馋记,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著懊烤,像睡著了一般梯醒。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上腌紧,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天茸习,我揣著相機(jī)與錄音,去河邊找鬼壁肋。 笑死号胚,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的浸遗。 我是一名探鬼主播猫胁,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼跛锌!你這毒婦竟也來了弃秆?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎菠赚,沒想到半個(gè)月后脑豹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡衡查,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年瘩欺,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拌牲。...
    茶點(diǎn)故事閱讀 40,030評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡击碗,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出们拙,到底是詐尸還是另有隱情稍途,我是刑警寧澤,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布砚婆,位于F島的核電站械拍,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏装盯。R本人自食惡果不足惜坷虑,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望埂奈。 院中可真熱鬧迄损,春花似錦、人聲如沸账磺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽垮抗。三九已至氏捞,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間冒版,已是汗流浹背液茎。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留辞嗡,地道東北人捆等。 一個(gè)月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像续室,于是被迫代替她去往敵國和親栋烤。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 一. 簡介 TextInput是一個(gè)允許用戶在應(yīng)用中通過鍵盤輸入文本的基本組件猎贴。本組件的屬性提供了多種特性的配置班缎,...
    飛奔的小馬閱讀 3,164評論 0 0
  • TextInput使用
    卡卡的RN閱讀 977評論 0 0
  • 文本輸入框是iOS開發(fā)中最常用的控件之一蝴光。Object-C中有UITextView和UITextfield,這兩個(gè)...
    Coder_Answer閱讀 10,402評論 0 5
  • View 個(gè)人感覺View就類似于html中的div標(biāo)簽达址,支持flexbox布局蔑祟。 一個(gè)簡單的練習(xí),類似攜程的格子...
    45b645c5912e閱讀 1,185評論 0 0
  • TextInput是一個(gè)允許用戶在應(yīng)用中通過鍵盤輸入文本的基本組件沉唠。本組件的屬性提供了多種特性的配置疆虚,譬如自動(dòng)完成...
    亭止閱讀 3,253評論 1 0