[React Native學(xué)習(xí)]之Text/TextInput

//居中
 alignItems:'center',
  justifyContent:'center'

組件的引用

定義組件的引用

通過某個組件的JSX代碼描述中加入ref={字符串},就可以定義一個組件的引用名稱

<TextInput ref='aReferName'
.....
/>

所以當(dāng)我們使用的時候弦追,就可以調(diào)用this.refs.aReferName得到這個組件的引用革答。

重新設(shè)定組件的屬性

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  TextInput,
  Text,
  View,
  Image,
} from 'react-native';

export default class Project21 extends Component {
  //構(gòu)造
  constructor(props){
    super(props);

    //初始狀態(tài)
    this.state = {
    textInputValue:''
  };
    this.buttonPressed = this.buttonPressed.bind(this);
  }

//當(dāng)按鈕按下的時候執(zhí)行此函數(shù)
  buttonPressed(){
    let textInputValue = 'new value';
    this.setState({textInputValue});

//調(diào)用組件的公開函數(shù)貌虾,修改文本輸入框的屬性值
    this.refs.textInputRefer.setNativeProps({
      editable:false
    });

//通過指向Text組件的引用
    this.refs.text2.setNativeProps({
      style:{
        color: 'blue',
        fontSize:30
      }
    });
  }
  render() {

    return (
      <View style={styles.container}> 
          <Text style={styles.buttonStyle}
                onPress={this.buttonPressed}>
                Press me genterly
          </Text>

          <Text style={styles.textPromptStyle}
                ref={'text2'}>   //指定本組名的引用名
                文字提示
          </Text>

          <View>
            <TextInput style={styles.textInputStyle}
                        ref={'textInputRefer'}
                        value={this.state.textInputValue}
                        onChageText={(textInputValue)=> this.setState({textInputValue})}/>
          </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor:'white'
  },

  buttonStyle:{ //文本組件樣式卡睦,使用該文本組件作為簡單的按鈕
    fontSize:20,
    backgroundColor:'grey'
  },

  textPromptStyle:{
    fontSize:20
  },
  textInputStyle:{
    width:150,
    height:50,
    fontSize:20,
    backgroundColor:'grey'
  }


});

AppRegistry.registerComponent('Project21', () => Project21);

獲取組件的位置

每一個React Native組件都有一個measure成員函數(shù)供炎,調(diào)用它可以得到組價當(dāng)前的寬工闺、高與位置信息

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  TextInput,
  View,
} from 'react-native';

export default class Project21 extends Component {

  constructor(props){
      super(props);
      //初始狀態(tài)
      this.state={};
      this.tempfunc = this.tempfunc.bind(this);
      this.getTextInputPosition = this.getTextInputPosition.bind(this);
  }

//在頁面被渲染之前執(zhí)行
  componentDidMount(){
    var aref = this.tempfunc;
    window.setTimeout(aref, 1);  //在compoinentDidMount執(zhí)行完后才可以獲取位置
    //因此指定一個1毫秒后超時的定時器
  }

  tempfunc(){
    this.refs.aTextInputRef.measure(this.getTextInputPosition);  //獲取位置
  }


  getTextInputPosition(fx, fy, width, height, px, py){
      console.log('getPosition');
        console.log("width:" + width); //控件寬
        console.log("height:" + height);//控件高
        console.log("fx:" + fx); //距離父控件左端 x的偏移量
        console.log("fy:" + fy); //距離父控件上端 y的偏移量
        console.log("px:" + px); //距離屏幕左端 x的偏移量
        console.log("py:" + py); //距離屏幕上端 y的偏移量
  }

  render() {

    return (
      <View style={styles.container}> 
          <View style={{borderWidth:1}}>
            <TextInput style={styles.textInputStyle}
              ref='aTextInputRef'
              defaultValue='Ajfg 你好'
              underlineColorAndroid='white'
            />
          </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor:'white'
  },

  buttonStyle:{ //文本組件樣式度苔,使用該文本組件作為簡單的按鈕
    fontSize:20,
    backgroundColor:'grey'
  },

  textPromptStyle:{
    fontSize:20
  },
  textInputStyle:{
    width:200,
    height:55,
    fontSize:50,
    alignItems: 'center',
    justifyContent: 'center',
    paddingTop:0,
    paddingBottom: 0
  }


});

AppRegistry.registerComponent('Project21', () => Project21);

跨平臺狀態(tài)欄組件

  • animated是布爾類型的屬性效拭,用來設(shè)定狀態(tài)欄的背景色暂吉、樣式和隱現(xiàn)被改變時,是否需要東阿虎效果缎患。他的默認(rèn)值是false
  • hidden是布爾類型的屬性慕的,用來設(shè)定狀態(tài)欄是否隱藏

Android特有屬性

  • backgroundColor
  • Android設(shè)備上狀態(tài)欄的背景顏色
  • translucent
  • 布爾類型,狀態(tài)欄是否半透明,如果為true,應(yīng)用將從物理頂端開始顯示
render() {

    return (
       <View style={{flex:1, backgroudColor: 'white', borderWidth:1 }}>

          <StatusBar
            animated={true}
            hidden={false}
            backgroundColor={'grey'}
            translucent={true}
            barStyle={'default'}
            //fade', 'slide'二選一,通過hidden屬性來顯示或隱藏狀態(tài)欄時所使用的動畫效果挤渔。默認(rèn)值為'fade'肮街。
            showHideTransition={'fade'}
            networkActivityIndicatorVisible={true}/>
      </View>
    );
  }

高度自增長的擴(kuò)展TextInput組件

export default class AutoExpandingTextInput extends Component {

  constructor(props){
    super(props);

    this.state={text:'',height:0};
    this._onChange=this._onChange.bind(this);
  }

  _onChange(event){
    this.setState({
      text:event.nativeEvent.text,
      height:event.nativeEvent.contentSize.height
    });
  }

  render() {

//multiline:是否能-顯示多行
    return (
       <TextInput {...this.props}  //將自定義的組件交給了TextInput
       multiline={true}
       onChange={this._onChange}
       style={[styles.textInputStyle, {height: Math.max(35, this.state.height)}]}
       value={this.state.text}/>
    );
  }
}

 class Project21 extends Component {

  _onChangeText(newText){
    console.log('inputed text:' + newText);
  }

  render() {

    return (
       <View style = {styles.container}>
        <AutoExpandingTextInput style={styles.textInputStyle}
                onChangeText={this._onChangeText}/>
       </View>
    );
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市判导,隨后出現(xiàn)的幾起案子嫉父,更是在濱河造成了極大的恐慌,老刑警劉巖眼刃,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绕辖,死亡現(xiàn)場離奇詭異,居然都是意外死亡擂红,警方通過查閱死者的電腦和手機(jī)仪际,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來昵骤,“玉大人树碱,你說我怎么就攤上這事”淝兀” “怎么了成榜?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蹦玫。 經(jīng)常有香客問我赎婚,道長雨饺,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任惑淳,我火速辦了婚禮额港,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘歧焦。我一直安慰自己移斩,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布绢馍。 她就那樣靜靜地躺著向瓷,像睡著了一般。 火紅的嫁衣襯著肌膚如雪舰涌。 梳的紋絲不亂的頭發(fā)上猖任,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天,我揣著相機(jī)與錄音瓷耙,去河邊找鬼朱躺。 笑死,一個胖子當(dāng)著我的面吹牛搁痛,可吹牛的內(nèi)容都是我干的长搀。 我是一名探鬼主播,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼鸡典,長吁一口氣:“原來是場噩夢啊……” “哼源请!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起彻况,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤谁尸,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后纽甘,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體良蛮,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年贷腕,在試婚紗的時候發(fā)現(xiàn)自己被綠了背镇。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片咬展。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡泽裳,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出破婆,到底是詐尸還是另有隱情涮总,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布祷舀,位于F島的核電站瀑梗,受9級特大地震影響烹笔,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜抛丽,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一谤职、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧亿鲜,春花似錦允蜈、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至垒探,卻和暖如春妓蛮,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背圾叼。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工蛤克, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人夷蚊。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓咖耘,卻偏偏與公主長得像,于是被迫代替她去往敵國和親撬码。 傳聞我的和親對象是個殘疾皇子儿倒,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,500評論 2 359

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