当前位置: 代码迷 >> Android >> Android中几种延后处理事件的步骤
  详细解决方案

Android中几种延后处理事件的步骤

热度:86   发布时间:2016-04-28 06:21:12.0
Android中几种延后处理事件的方法
[java]?view plaincopy
?
  1. 一、Handler和TimerTask相结合??

1、首先定义TimerTask

[java]?view plaincopy
?
  1. TimerTask?task?=?new?TimerTask(){????
  2. ??public?void?run()?{????
  3. ??????Message?message?=?new?Message();????????
  4. ??????message.what?=?1;????????
  5. ??????handler.sendMessage(message);??????
  6. ??}????
  7. ??????
  8. };??


2、定义Handler

?

[java]?view plaincopy
?
  1. Handler?handler?=?new?Handler()?{??
  2. ????public?void?handleMessage(Message?msg)?{??
  3. ????????switch?(msg.what)?{??
  4. ????????case?1:??
  5. ????????????break;??
  6. ????????}??
  7. ????????super.handleMessage(msg);??
  8. ????}??
  9. };??


3、用timer调用事件

[java]?view plaincopy
?
  1. Timer?timer?=?new?Timer();???
  2. timer.schedule(task,?50);??


二、Android中自带的postDelayed

1、android view中的postDelayed

[java]?view plaincopy
?
  1. v.postDelayed(new?Runnable()?{??
  2. ????????????????????public?void?run()?{??
  3. ?????
  4. ????????????????????}??
  5. ????????????????},?30);??


2、Handler中的postDelayed

[java]?view plaincopy
?
  1. handler.postDelayed(new?Runnable()?{??
  2. ????????????????????public?void?run()?{??
  3. ?????
  4. ????????????????????}??
  5. ????????????????},?30); ?
  相关解决方案