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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章