React Native(iOS)新手小白零基礎(chǔ)自學(xué)(四)TextInput組件

TextInput 類(lèi)似于iOS的UITextField,也是常用控件惋戏。先簡(jiǎn)單介紹一下基本屬性:

      autoCapitalize: 枚舉類(lèi)型,可選值有'none'表鳍、'sentences'骇窍、‘words’、‘characters’包吝,當(dāng)用戶(hù)輸入時(shí)用于提示
      placeholder:占位符饼煞,在輸入前顯示的文本
      value:文本輸入框的默認(rèn)值
      placeholderTextColor:占位符文本的顏色
      password:如果為true,則是密碼輸入框诗越,文本顯示為‘*’
      multiline:如果為true砖瞧,則是多行輸入
      editable:如果為false,文本框不可輸入嚷狞,默認(rèn)為true
      autoFocus:如果為true块促,將自動(dòng)聚焦
      clearButtonMode:枚舉類(lèi)型,可選值有‘never’床未、‘while-editing’竭翠、‘unless-editing’、‘a(chǎn)lways’薇搁,用以顯示清除按鈕
      maxLength:能夠輸入的最長(zhǎng)字符數(shù)
      enablesReturnKeyAutomatically:如果值為true斋扰,表示沒(méi)有文本時(shí)鍵盤(pán)是不能有返回鍵的,默認(rèn)為false
      returnKeyType:枚舉類(lèi)型,可選值有‘default’传货、‘go’屎鳍、‘google’、‘join’问裕、‘next’哥艇、‘route’、‘search’僻澎、‘senvar React = require('react-native');

以高德搜索提示為例貌踏,如圖:


屏幕快照 2016-05-09 上午9.44.36.png
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TextInput
} = React;

var Search = React.createClass ({
  render:function() {
    return (
      <View style={[styles.flex, styles.flexDirection]}>
        <View style={styles.flex}>
          <TextInput style={styles.input} returnKeyType="search"/>
        </View>
        <View style={styles.btn}>
          <Text style={styles.search}>搜索</Text>
        </View>
      </View>
    );
  }
});

var TI = React.createClass ({
  render:function() {
    return (
      <View style={[styles.flex, styles.topStatus]}>
        <Search></Search>
      </View>
    );
  }
});

var styles = StyleSheet.create ({
  flex: {
    flex:1
  },
  flexDirection: {
    flexDirection:'row'
  },
  btn: {
    width:55,
    marginLeft:-5,
    marginRight:5,
    backgroundColor:'#23BEFF',
    height:45,
    justifyContent:'center',
    alignItems:'center'
  },
  input: {
    height:45,
    borderWidth:1,
    marginLeft:5,
    paddingLeft:5, //TextInput輸入內(nèi)容相對(duì)外邊框起點(diǎn)
    borderColor:'#ccc',
    borderRadius:4
  },
  search: {
    color:'#fff',
    fontSize:15,
    fontWeight:'bold'
  },
  topStatus: {
    marginTop:25
  }
});

AppRegistry.registerComponent('InformationServicesRN', () => TI);

2.自動(dòng)提示列表

var onePT = 1 / React.PixelRatio.get();
var Search = React.createClass ({

  getInitialState:function() {
    return {
      show:false
    };
  },
  getValue:function(text) {
    var value = text;
    this.setState({
      show:true,
      value:value
    });
  },
  hide:function(val) {
    this.setState({
      show:false,
      value:val
    });
  },

  render:function() {
    return (
      <View style={styles.flex}>
      <View style={[styles.inputHeight, styles.flexDirection]}>
        <View style={styles.flex}>
          <TextInput style={styles.input}
                     returnKeyType="search"
                     placeholder="請(qǐng)輸入關(guān)鍵字"
                     onEndEditing={this.hide.bind(this, this.state.value)}
                     value={this.state.value}
                     onChangeText={this.getValue}/>
        </View>
        <View style={styles.btn}>
          <Text style={styles.search}
                 onPress={this.hide.bind(this, this.state.value)}>搜索</Text>
        </View>
      </View>

      {this.state.show ?
        <View style={styles.result}>
          <Text onPress={this.hide.bind(this, this.state.value + '莊')}
                style={styles.item}
                numberOfLines={1}>{this.state.value}莊</Text>
          <Text onPress={this.hide.bind(this, this.state.value + '園街')}
                style={styles.item}
                numberOfLines={1}>{this.state.value}園街</Text>
          <Text onPress={this.hide.bind(this, 80 + this.state.value + '綜合商店')}
                style={styles.item}
                numberOfLines={1}>80{this.state.value}綜合商店</Text>
          <Text onPress={this.hide.bind(this, this.state.value + '桃')}
                style={styles.item}
                numberOfLines={1}>{this.state.value}桃</Text>
          <Text onPress={this.hide.bind(this, '楊林' + this.state.value + '園')}
                style={styles.item}
                numberOfLines={1}>楊林{this.state.value}園</Text>
        </View>
        : null
      }
      </View>
    );
  },
});

樣式:

item: {
    fontSize:16,
    padding:5,
    paddingTop:10,
    paddingBottom:10,
    borderWidth:onePT,
    borderColor:'#ddd',
    borderTopWidth:onePT
  },
  result: {
    marginTop:onePT,
    marginLeft:5,
    marginRight:5,
    height:200,
    borderColor:'#ccc',
    borderTopWidth:onePT,
    // backgroundColor:'red'
  }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市窟勃,隨后出現(xiàn)的幾起案子祖乳,更是在濱河造成了極大的恐慌,老刑警劉巖秉氧,帶你破解...
    沈念sama閱讀 219,039評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件眷昆,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡汁咏,警方通過(guò)查閱死者的電腦和手機(jī)亚斋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)攘滩,“玉大人帅刊,你說(shuō)我怎么就攤上這事∑剩” “怎么了赖瞒?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,417評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)蚤假。 經(jīng)常有香客問(wèn)我栏饮,道長(zhǎng),這世上最難降的妖魔是什么磷仰? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,868評(píng)論 1 295
  • 正文 為了忘掉前任袍嬉,我火速辦了婚禮,結(jié)果婚禮上灶平,老公的妹妹穿的比我還像新娘伺通。我一直安慰自己,他們只是感情好民逼,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布泵殴。 她就那樣靜靜地躺著,像睡著了一般拼苍。 火紅的嫁衣襯著肌膚如雪笑诅。 梳的紋絲不亂的頭發(fā)上调缨,一...
    開(kāi)封第一講書(shū)人閱讀 51,692評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音吆你,去河邊找鬼弦叶。 笑死,一個(gè)胖子當(dāng)著我的面吹牛妇多,可吹牛的內(nèi)容都是我干的伤哺。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼者祖,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼立莉!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起七问,我...
    開(kāi)封第一講書(shū)人閱讀 39,326評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤蜓耻,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后械巡,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體刹淌,經(jīng)...
    沈念sama閱讀 45,782評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評(píng)論 3 337
  • 正文 我和宋清朗相戀三年讥耗,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了有勾。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,102評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡古程,死狀恐怖蔼卡,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情籍琳,我是刑警寧澤菲宴,帶...
    沈念sama閱讀 35,790評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站趋急,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏势誊。R本人自食惡果不足惜呜达,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望粟耻。 院中可真熱鬧查近,春花似錦、人聲如沸挤忙。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,996評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)册烈。三九已至戈泼,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背大猛。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,113評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工扭倾, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人挽绩。 一個(gè)月前我還...
    沈念sama閱讀 48,332評(píng)論 3 373
  • 正文 我出身青樓膛壹,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親唉堪。 傳聞我的和親對(duì)象是個(gè)殘疾皇子模聋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評(píng)論 2 355

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