React-Native中使用驗證碼倒計時的按鈕實例代碼
開發(fā)過程中有獲取手機(jī)驗證碼的場景,這時候有這樣的要求:
1,點擊“獲取驗證碼”的按鈕,發(fā)起獲取驗證碼的網(wǎng)絡(luò)請求,同時按鈕置為不可用
2,如果網(wǎng)絡(luò)請求成功,按鈕繼續(xù)不可用,但按鈕上文本改為倒計時((*s)后重新獲取)
3,如果網(wǎng)絡(luò)請求失敗,按鈕置為可用
4,倒計時結(jié)束,按鈕可用
直接上代碼
源碼
import React,{PropTypes} from 'react'; import {View,Text,TouchableOpacity} from 'react-native'; export default class TimerButton extends React.Component { constructor(props) { super(props) this.state = { timerCount: this.props.timerCount || 90, timerTitle: this.props.timerTitle || '獲取驗證碼', counting: false, selfEnable: true, }; this._shouldStartCountting = this._shouldStartCountting.bind(this) this._countDownAction = this._countDownAction.bind(this) } static propTypes = { style: PropTypes.object, textStyle: Text.propTypes.style, onClick: PropTypes.func, disableColor: PropTypes.string, timerTitle: PropTypes.string, enable: React.PropTypes.oneOfType([React.PropTypes.bool,React.PropTypes.number]) }; _countDownAction(){ const codeTime = this.state.timerCount; this.interval = setInterval(() =>{ const timer = this.state.timerCount - 1 if(timer===0){ this.interval&&clearInterval(this.interval); this.setState({ timerCount: codeTime, timerTitle: this.props.timerTitle || '獲取驗證碼', counting: false, selfEnable: true }) }else{ console.log("---- timer ",timer); this.setState({ timerCount:timer, timerTitle: `重新獲取(${timer}s)`, }) } },1000) } _shouldStartCountting(shouldStart){ if (this.state.counting) {return} if (shouldStart) { this._countDownAction() this.setState({counting: true,selfEnable:false}) }else{ this.setState({selfEnable:true}) } } componentWillUnmount(){ clearInterval(this.interval) } render(){ const {onClick,style,textStyle,enable,disableColor} = this.props const {counting,timerTitle,selfEnable} = this.state return ( <TouchableOpacity activeOpacity={counting ? 1 : 0.8} onPress={()=>{ if (!counting && enable && selfEnable) { this.setState({selfEnable:false}) this.props.onClick(this._shouldStartCountting) }; }}> <View style={[{width:100, height:44,flex:1,justifyContent:'center',alignItems:'center'},style]}> <Text style={[{fontSize: 16},textStyle,{color: ((!counting && enable && selfEnable) ? textStyle.color : disableColor || 'gray')}]}>{timerTitle}</Text> </View> </TouchableOpacity> ) } }
使用
<TimerButton enable={phoneNumber.length} style={{width: 110,marginRight: 10}} textStyle={{color: StaticColor.COLOR_MAIN}} timerCount={10} onClick={(shouldStartCountting)=>{ this._requestSMSCode(shouldStartCountting) }}/>
onClick
:觸發(fā)后按鈕selfEnable
會立即被置為false
- 通過
onClick
中的一系列邏輯處理之后需要調(diào)用回調(diào)函數(shù)結(jié)束倒計時 shouldStartCountting
:回調(diào)函數(shù),接受一個Bool
類型的參數(shù)shouldStartCountting(true)
,開始倒計時,倒計時結(jié)束時自動恢復(fù)初始狀態(tài)shouldStartCountting(false)
, 按鈕的selfEnable
會立即被置為true
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實現(xiàn)開機(jī)自動啟動Service或app的方法
這篇文章主要介紹了Android實現(xiàn)開機(jī)自動啟動Service或app的方法,結(jié)合實例形式分析了Android開機(jī)自啟動程序的具體步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2016-07-07Android入門之ViewFlipper翻轉(zhuǎn)視圖的使用詳解
本篇給大家?guī)Я说氖荲iewFlipper,它是Android自帶的一個多頁面管理控件,且可以自動播放!本篇我們我們會使用兩個例子:一個自動播放首頁輪播頁一個手動可左右滑動道頁的輪播頁來說透這個組件的使用,感興趣的可以了解一下2022-11-11Android開發(fā)環(huán)境搭建圖文教程 親測有效!
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)環(huán)境搭建圖文教程,親自測試有效的搭建方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03Android實戰(zhàn)打飛機(jī)游戲之子彈生成與碰撞以及爆炸效果(5)
這篇文章主要為大家詳細(xì)介紹了Android實戰(zhàn)打飛機(jī)游戲之子彈生成與碰撞以及爆炸效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07使用Android Studio實現(xiàn)為系統(tǒng)級的app簽名
這篇文章主要介紹了使用Android Studio實現(xiàn)為系統(tǒng)級的app簽名,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03