轉載時請記得標明源地址: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); } }