onFinish();
return this;
}
// 停止时间 = 系统启动时间 + 总计时间
mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
mHandler.sendMessage(mHandler.obtainMessage(MSG));
return this;
}
/**
-
Callback fired on regular interval.
-
@param millisUntilFinished The amount of time until finished.
*/
public abstract void onTick(long millisUntilFinished);
/**
- Callback fired when the time is up.
*/
public abstract void onFinish();
private static final int MSG = 1;
// handles counting down
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
synchronized (CountDownTimer.this) {
// 计算剩余总时间
final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();
// 小于等于 0 ,回调 onFinish
if (millisLeft <= 0) {
onFinish();
} else if (millisLeft < mCountdownInterval) { // 小于计时间隔 ,delayed 一个消息
// no tick, just delay until done
sendMessageDelayed(obtainMessage(MSG), millisLeft);
} else {
long lastTickStart = SystemClock.elapsedRealtime();
onTick(millisLeft);
// take into account user’s onTick taking time to execute
long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();
// special case: user’s onTick took more than interval to
// complete, skip to next interval
while (delay < 0) delay += mCountdownInterval;
sendMessageDelayed(obtainMessage(MSG), delay);
}
}
}
};
使用:倒计时
final CountDownTimer timer = new CountDownTimer(60000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
get_verfiy_code.setText(millisUntilFinished/1000 + “秒”);
}
@Override
public void onFinish() {
get_verfiy_code.setEnabled(true);
get_verfiy_code.setText(“获取验证码”);
}
};
点击按钮的时候:
get_verfiy_code.setEnabled(false);
timer.start();
popupwinow全部如下:
/**
-
Created by
-
xiaomi on 2017/9/8.
*/
public class VerifyPopupWindow {
/**
- 手机验证popupwindow
*/
private EditText phone_number;
private EditText input_image_code;
private ImageView image_code;
private EditText input_code;
private Button sure;
private String inputcode;
private String realCode;
private Button no_verify;
private CustomPopWindow mPopWindow;
private Button get_verfiy_code;
private Context mContext;
public VerifyPopupWindow(Context context){
this.mContext=context;
}
public void showPopupWindow(){
View contentView = LayoutInflater.from(mContext).inflate(R.layout.user_phone_number, null);
phone_number = (EditText) contentView.findViewById(R.id.phone_number); //填入手机号
input_image_code=(EditText)contentView.findViewById(R.id.input_image_code);//填入图片验证码
image_code=(ImageView)contentView.findViewById(R.id.image_code); //图片验证码
input_code=(EditText) contentView.findViewById(R.id.input_code); //填入手机验证码
sure=(Button)contentView.findViewById(R.id.user_verfiy_submit); //确定绑定
no_verify=(Button)contentView.findViewById(R.id.ignore_verfiy_submit); //暂不验证
get_verfiy_code=(Button)contentView.findViewById(R.id.get_verfiy_code);//获取手机验证码
final CountDownTimer timer = new CountDownTimer(60000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
get_verfiy_code.setText(millisUntilFinished/1000 + “秒”);
}
@Override
public void onFinish() {
get_verfiy_code.setEnabled(true);
get_verfiy_code.setText(“获取验证码”);
}
};
image_code.setImageBitmap(Code.getInstance().createBitmap()); //初始化验证码
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.image_code:
image_code.setImageBitmap(Code.getInstance().createBitmap());
break;
case R.id.user_verfiy_submit:
if (TextUtils.isEmpty(phone_number.getText().toString())){
Toast.makeText(mContext,“请您输入手机号码”, Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(input_code.getText().toString())){
Toast.makeText(mContext,“请您输入手机验证码”, Toast.LENGTH_SHORT).show();
return;
}
PostFormBuilder builder= OkHttpUtils.post();
builder
.addParams(“tel”,phone_number.getText().toString())
.addParams(“telyzm”,input_code.getText().toString());
builder.build()
.execute(new StringCallback() {
@Override
public void one rror(Call call, Exception e, int id) {
}
@Override
public void onResponse(String response, int id) {
try{
JSONObject jsonObject=new JSONObject(response);
int staus=jsonObject.getInt(“staus”);
if (staus==1){
mPopWindow.dissmiss();
Toast.makeText(mContext,“恭喜您,账号绑定成功!”, Toast.LENGTH_SHORT).show();
}else {
String message=jsonObject.getString(“msg”);
if (message!=null){
Toast.makeText(mContext,message, Toast.LENGTH_SHORT).show();
}
}
}catch (Exception e){
e.printStackTrace();
}
}
});
break;
case R.id.ignore_verfiy_submit:
mPopWindow.dissmiss();
break;
case R.id.get_verfiy_code:
// MyCountDownTimer.start();
inputcode=input_image_code.getText().toString();
realCode = Code.getInstance().getCode();
if (TextUtils.isEmpty(phone_number.getText().toString())){
Toast.makeText(mContext,“请您输入手机号码”, Toast.LENGTH_SHORT).show();
return;
}
if (realCode.equalsIgnoreCase(inputcode)){
//发送手机信息到服务器,通知服务端发验证码,并获取验证码。
getVerifyCode(phone_number.getText().toString());
}else{
Toast.makeText(mContext,“您输入的验证码有误,请重新输入”, Toast.LENGTH_SHORT).show();
}
get_verfiy_code.setEnabled(false);
timer.start();
break;
}
}
};
image_code.setOnClickListener(listener);
sure.setOnClickListener(listener);
no_verify.setOnClickListener(listener);
get_verfiy_code.setOnClickListener(listener);
mPopWindow = new CustomPopWindow.PopupWindowBuilder(mContext)
.setView(contentView)
.enableOutsideTouchableDissmiss(false)// 设置点击PopupWindow之外的地方,popWindow不关闭,如果不设置这个属性或者为tr
ue,则关闭
.enableBackgroundDark(true) //弹出popWindow时,背景是否变暗
.setBgDarkAlpha(0.7f) // 控制亮度
.setOnDissmissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
Log.e(“TAG”,“onDismiss”);
}
})
.create();
mPopWindow.showAtLocation(contentView, Gravity.CENTER, 0, 0);
}
//把手机后台获取手机验证码
public void getVerifyCode(String phoneNumber){
try {
PostFormBuilder builder=OkHttpUtils.post().url("");
builder
.addParams(“tel”,phoneNumber);
builder
.build()
.execute(new StringCallback() {
@Override
public void one rror(Call call, Exception e, int id) {
}
@Override
public void onResponse(String response, int id) {
try {
JSONObject jsonObject=new JSONObject(response);
int staus=jsonObject.getInt(“staus”);
String message=jsonObject.getString(“msg”);
if (staus==1){
Toast.makeText(mContext,message, Toast.LENGTH_SHORT).show();
}else {
if (message!=null){
Toast.makeText(mContext,message, Toast.LENGTH_SHORT).show();
}
}
}catch (Exception e){
e.printStackTrace();
}
}
});
}catch (Exception e){
e.printStackTrace();
}
{
}
@Override
public void onResponse(String response, int id) {
try {
JSONObject jsonObject=new JSONObject(response);
int staus=jsonObject.getInt(“staus”);
String message=jsonObject.getString(“msg”);
if (staus==1){
Toast.makeText(mContext,message, Toast.LENGTH_SHORT).show();
}else {
if (message!=null){
Toast.makeText(mContext,message, Toast.LENGTH_SHORT).show();
}
}
}catch (Exception e){
e.printStackTrace();
}
}
});
}catch (Exception e){
e.printStackTrace();
}