Android控件postDelayed用法,View自带的定时器
有一个需求是这样的,点击加关注按钮后,执行关注操作,成功后按钮文字变为“已关注”,保持3秒,三秒后按钮文字便问“取消关注”,点击后执行取消关注的操作
源码:
public boolean postDelayed(Runnable action, long delayMillis) { final AttachInfo attachInfo = mAttachInfo; if (attachInfo != null) { return attachInfo.mHandler.postDelayed(action, delayMillis); } // Assume that post will succeed later ViewRootImpl.getRunQueue().postDelayed(action, delayMillis); return true;
为控件设置延迟属性
tvAttentionTa.setText("已关注"); /*3秒内设置不可点击*/ tvAttentionTa.setClickable(false); tvAttentionTa.postDelayed(new Runnable() { @Override public void run() { /*3秒后可以点击*/ tvAttentionTa.setClickable(true); tvAttentionTa.setText("取消关注"); } },3*1000);