亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

自定義單選按鈕 React Native

自定義單選按鈕 React Native

哈士奇WWW 2021-11-04 16:43:48
嘿,所以我是本機和 javascript 的新手,目前我正在學習制作一個帶有圖像的自定義單選按鈕,它看起來像這樣我在此頁面中的自定義單選按鈕用戶將從列表中選擇一個按鈕,我想要在頁面第一次渲染時顯示一個按下的按鈕,并且用戶只能選擇一個按鈕。誰能告訴我如何解決這個問題?提前致謝這是我的代碼單選按鈕.jsexport default class RadioButton extends Component {    constructor(props) {        super(props);        this.state = {            selected: this.props.currentSelection === this.props.value,        }    }    button() {        var imgSource = this.state.selected? this.props.normalImg : this.props.selectedImg;        return (          <Image            source={ imgSource }          />        );      }      render() {        let activeButton = this.props.activeStyle ? this.props.activeStyle : styles.activeButton;        return (            <View style={[styles.container, this.props.containerStyle, this.state.selected || this.props.normalImg ? activeButton : inactiveButton]}>                <TouchableOpacity onPress={() => {                    this.setState({ selected: !this.state.selected });                                    }}>                    {                       this.button()                    }                </TouchableOpacity>            </View>        );    }}活動日志.jsclass ActivityLog extends Component {    constructor(props){        super(props);     }
查看完整描述

1 回答

?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

將activeStatus提取到ActivityLog中來跟蹤哪個按鈕被選中,現在你為每個按鈕維護一個狀態作為本地狀態。所以很難知道其他組件知道按鈕的活動狀態。


這是一個粗略的想法的通用實現。


const Child=(props)=>{

    <TouchableOpacity onPress={props.handlePress}>

        <Text style={[baseStyle,active && activeStyle]}>{props.title}</Text>

    </TouchableOpacity>

}

class Parent extends React.Component{

 state={

  selectedChild:''

 }

 changeSelection=(sometitle)=>{

  this.setState({

      selectedChild:sometitle

  })

 }

 render(){

     return(

         <View>

             <Child handlePress={()=>this.changeSelection('1')} active={this.state.selectedChild==='1'}/>

             <Child handlePress={()=>this.changeSelection('2')} active={this.state.selectedChild==='2'}/>

             <Child handlePress={()=>this.changeSelection('3')} active={this.state.selectedChild==='3'}/>

             <Child handlePress={()=>this.changeSelection('4')} active={this.state.selectedChild==='4'}/>

         </View>

     )

 }

}


查看完整回答
反對 回復 2021-11-04
  • 1 回答
  • 0 關注
  • 279 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號