/**
* 注释 :自定义可随意显示时长的Toast
*/
[AppleScript] 纯文本查看 复制代码 public class MyToast {
Toast taost = null;
Context mContext;
String str;
int i;
private long firstTime;
private Timer timer;
/**
*
* @param context
* @param str 要显示的内容
* @param i 要显示的时长
*/
public MyToast(Context context, String str, int i) {
this.mContext = context;
this.str = str;
this.i = i;
}
MyTimerTask timerTask;
public void showToast() {
firstTime = System.currentTimeMillis();
timerTask = new MyTimerTask();
timer = new Timer(true);
timer.schedule(timerTask, 0, 2000);//定时每秒执行一次
}
//定时任务,定时发送message
private class MyTimerTask extends TimerTask {
@Override
public void run() {
Message message = Message.obtain();
message.what = 1;
if (System.currentTimeMillis() - firstTime < i) {
System.out.println("----------" + (System.currentTimeMillis() - firstTime));
mHandler.sendMessage(message); //发送message
} else {
timer.cancel();
System.out.println("quxiao toast");
return;
}
}
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
if (taost != null) {
taost.cancel();
taost.makeText(mContext, str, Toast.LENGTH_LONG).show();
}
taost.makeText(mContext, str, Toast.LENGTH_LONG).show();
}
}
};
}
传智播客·黑马程序员郑州校区地址 河南省郑州市 高新区长椿路11号大学科技园(西区)东门8号楼三层 联系电话 0371-56061160/61/62 来校路线 地铁一号线梧桐街站A口出
|