React-Native中使用驗證碼倒計時的按鈕實例代碼
開發(fā)過程中有獲取手機驗證碼的場景,這時候有這樣的要求:
1,點擊“獲取驗證碼”的按鈕,發(fā)起獲取驗證碼的網(wǎng)絡請求,同時按鈕置為不可用
2,如果網(wǎng)絡請求成功,按鈕繼續(xù)不可用,但按鈕上文本改為倒計時((*s)后重新獲取)
3,如果網(wǎng)絡請求失敗,按鈕置為可用
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é)束時自動恢復初始狀態(tài)shouldStartCountting(false), 按鈕的selfEnable會立即被置為true
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android 關于ExpandableListView刷新問題的解決方法
下面小編就為大家?guī)硪黄狝ndroid 關于ExpandableListView刷新問題的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
Android Studio查看Android 5.x源碼的步驟詳解
Google為Android開發(fā)者帶來Android Studio,用來取代Eclipse。從Android Studio出現(xiàn)起,整機開發(fā)和Android源碼閱讀和編輯一定能用上它。這篇文章小編就帶大家學習下如何使用Android Studio查看Android 5.x源碼,有需要的可以參考借鑒。2016-09-09
Windows下搭建Android開發(fā)環(huán)境
這篇文章主要介紹了Windows下搭建Android開發(fā)環(huán)境,需要的朋友可以參考下2015-09-09
淺談Android實踐之ScrollView中滑動沖突處理解決方案
涉及到了ViewPager,MapView,ListView,就需要ScrollView來做一下支援,這篇文章主要介紹了淺談Android實踐之ScrollView中滑動沖突處理解決方案,有需要的可以來了解一下。2016-12-12
Android基于自帶的DownloadManager實現(xiàn)下載功能示例
這篇文章主要介紹了Android基于自帶的DownloadManager實現(xiàn)下載功能,結(jié)合實例形式分析了DownloadManager實現(xiàn)下載功能的具體操作步驟與相關注意事項,需要的朋友可以參考下2017-08-08
Android變形(Transform)之Camera使用介紹
Camera主要實現(xiàn)3D的變形,有轉(zhuǎn)動,旋轉(zhuǎn)等,Camera的源碼是由Native(本地代碼)實現(xiàn),提供的接口也比較簡單,感興趣的朋友可以參考下,或許對你學習有所幫助2013-02-02
Android使用ImageView實現(xiàn)支持手勢縮放效果
這篇文章主要介紹了Android使用ImageView實現(xiàn)支持手勢縮放效果,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09

