有一个需求是这样的,点击加关注按钮后,执行关注操作,成功后按钮文字变为“已关注”,保持3秒,三秒后按钮文字便问“取消关注”,点击后执行取消关注的操作
可以使用定时器实现,但是使用View的posyDelayed更加方便
源码如下:
android.view.View
1
2
3
4
5
6
7
8
9
|
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
;
}
|
testTv.postDelay(
new
Runnable(){
public
void
run()
{
//do something
}
},
2
*
1000
);
tvAttentionTa.setText("已关注"); /*3秒内设置不可点击*/ tvAttentionTa.setClickable(false); tvAttentionTa.postDelayed(new Runnable() { @Override public void run() { /*3秒后可以点击*/ tvAttentionTa.setClickable(true); tvAttentionTa.setText("取消关注"); } },3*1000);