小視訊原始碼,自定義倒計時,結束後進入重新發送介面
阿新 • • 發佈:2022-05-11
小視訊原始碼,自定義倒計時,結束後進入重新發送介面
<Button
android:id="@+id/rebind_sms_btn"
android:layout_width="120dp"
android:layout_height="45dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@null"
android:gravity="center"
android:text="獲取簡訊驗證碼"
android:textColor="#059CD4"
android:textSize="16sp" />
自定義倒計時時間類:
import android.os.CountDownTimer;
import android.widget.TextView;
public class MyCountTimer extends CountDownTimer {
public static final int TIME_COUNT = 121000;// 時間防止從119s開始顯示(以倒計時120s為例子)
private TextView btn;
private int endStrRid;
private int normalColor, timingColor;// 未計時的文字顏色,計時期間的文字顏色
/**
* 引數 millisInFuture 倒計時總時間(如60S,120s等) 引數 countDownInterval 漸變時間(每次倒計1s)
*
* 引數 btn 點選的按鈕(因為Button是TextView子類,為了通用我的引數設定為TextView)
*
* 引數 endStrRid 倒計時結束後,按鈕對應顯示的文字
*/
public MyCountTimer(long millisInFuture, long countDownInterval,
TextView btn, int endStrRid) {
super(millisInFuture, countDownInterval);
this.btn = btn;
this.endStrRid = endStrRid;
}
/**
*
* 引數上面有註釋
*/
public MyCountTimer(TextView btn, int endStrRid) {
super(TIME_COUNT, 1000);
this.btn = btn;
this.endStrRid = endStrRid;
}
public MyCountTimer(TextView btn) {
super(TIME_COUNT, 1000);
this.btn = btn;
this.endStrRid = R.string.txt_getMsgCode_validate;
}
public MyCountTimer(TextView tv_varify, int normalColor, int timingColor) {
this(tv_varify);
this.normalColor = normalColor;
this.timingColor = timingColor;
}
// 計時完畢時觸發
@Override
public void onFinish() {
if (normalColor > 0) {
btn.setTextColor(normalColor);
}
btn.setText(endStrRid);
btn.setEnabled(true);
}
// 計時過程顯示
@Override
public void onTick(long millisUntilFinished) {
if (timingColor > 0) {
btn.setTextColor(timingColor);
}
btn.setEnabled(false);
btn.setText(millisUntilFinished / 1000 + "秒");
}
}
使用:
public class MainActivity extends Activity {
private Button rebind_sms_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rebind_sms_btn=(Button) findViewById(R.id.rebind_sms_btn);
rebind_sms_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// txt_getMsgCode_validate//定義為重新發送
MyCountTimer myCountTimer=new MyCountTimer(60000, 1000,rebind_sms_btn,R.string.txt_getMsgCode_validate );
myCountTimer.start();
}
});
}
}
以上就是 小視訊原始碼,自定義倒計時,結束後進入重新發送介面,更多內容歡迎關注之後的文章