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

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

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


//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.
 * <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);
    }
}

看了 源码是不是感觉懂了呢!!!


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);
    }
}

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值