Android 验证码倒计时 与 getDrawable(int) 方法过时问题

转载时请记得标明源地址:https://my.oschina.net/lijindou/blog/714657android

本人博客地址:http://my.oschina.net/lijindou/blogide

 

//android 封装的一个倒计时的一个类,第一个参数表示总时间,第二个参数表示间隔时间。意思就是每隔一秒会回调一次方法onTick,而后6秒以后会回调onFinish方法。ui

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() {
         //这块是 时间过完的处理
        }
    };

调用的时候直接用    this

timer.start();

就能够了,感受 真心好用啊!!!.net

今天我写的时候发现  在 AS 上用的时候发现   res.getDrawable(R.drawable.but_send_code);这个方法过时了  而后 我在就在 开源中国的技术问答中有人给我回答了,code

用这个方法替代:orm

drawable = ContextCompat.getDrawable(LoginActivity.this,R.drawable.but_send_code);blog

就能够了  ,  缘由很简单,看看 源码就知道了,下面是源码:ci

/**
 * Return a drawable object associated with a particular resource ID.
 * <p>
 * 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);
    }
}

看了 源码是否是感受懂了呢!!!get

 

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
 * <p>
 * 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);
    }
}
相关文章
相关标签/搜索