//android 封装的一个倒计时的一个类,第一个参数表示总时间,第二个参数表示间隔时间。意思就是每隔一秒会回调一次方法onTick,然后6秒之后会回调onFinish方法。
private CountDownTimer timer = new CountDownTimer(6000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
but_login_password_get_user_code.setText((millisUntilFinished / 1000) + " s");
//这块是 每过一秒的处理
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onFinish() {
//这块是 时间过完的处理
}
};
调用的时候直接用
timer.start();
就可以了,感觉 真心好用啊!!!
今天我写的时候发现 在 AS 上用的时候发现 res.getDrawable(R.drawable.but_send_code);这个方法过期了 然后 我在就在 开源中国的技术问答中有人给我回答了,
用这个方法替代:
drawable = ContextCompat.getDrawable(LoginActivity.this,R.drawable.but_send_code);
就可以了 , 原因很简单,看看 源码就知道了,下面是源码:
/**
* Return a drawable object associated with a particular resource ID.
*
* Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the returned
* drawable will be styled for the specified Context's theme.
*
* @param id The desired resource identifier, as generated by the aapt tool.
* This integer encodes the package, type, and resource entry.
* The value 0 is an invalid identifier.
* @return Drawable An object that can be used to draw this resource.
*/
public static final Drawable getDrawable(Context context, int id) {
final int version = Build.VERSION.SDK_INT;
if (version >= 21) {
return ContextCompatApi21.getDrawable(context, id);
} else {
return context.getResources().getDrawable(id);
}
}
看了 源码是不是感觉懂了呢!!!
2016/07/25
同理我发现
getResources().getColor(R.color.dark_gray)
这个方法也是过期了,替代的方法也是
ContextCompat.getColor(MainActivity.this,R.color.dark_gray)
下面是源码:
/**
* Returns a color associated with a particular resource ID
*
* Starting in {@link android.os.Build.VERSION_CODES#M}, the returned
* color will be styled for the specified Context's theme.
*
* @param id The desired resource identifier, as generated by the aapt
* tool. This integer encodes the package, type, and resource
* entry. The value 0 is an invalid identifier.
* @return A single color value in the form 0xAARRGGBB.
* @throws android.content.res.Resources.NotFoundException if the given ID
* does not exist.
*/
public static final int getColor(Context context, int id) {
final int version = Build.VERSION.SDK_INT;
if (version >= 23) {
return ContextCompatApi23.getColor(context, id);
} else {
return context.getResources().getColor(id);
}
}