首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

兑现长期显示的toast

2012-09-20 
实现长期显示的toastpublic class CustomToast{private Handler handlerpublic Toast toastprivate int

实现长期显示的toast

public class CustomToast{private Handler handler;public Toast toast;private int LONG_SHOW=-1;private boolean isShowing=false;public CustomToast(Context context) {this(context,null);}public CustomToast(Context context,Toast toast){if(null==toast) this.toast=new Toast(context);else this.toast=toast;handler = new Handler();}public void show(){Log.i("show","<<<<<<<<<<>>>>>>>>>>>>>>");isShowing=true;if(toast.getDuration()==LONG_SHOW){toast.setDuration(Toast.LENGTH_LONG);toast.show();showUntilHide();}else{toast.show();}}public void hide(){Log.i("hide","<<<<<<<<<<>>>>>>>>>>>>>>");if(null!=toast){toast.cancel();}isShowing=false;}public boolean isShow(){return isShowing;}private void showUntilHide(){Log.i("showUntileHide","<<<<<<<<<<>>>>>>>>>>>>>>");if(null==toast) return;if(isShowing){toast.show();handler.postDelayed(r, 3000);}}Runnable r = new Runnable(){public void run(){showUntilHide();}};}

?

调用方法:

?

Toast toast = new Toast(TestAndroid.this);toast.setDuration(-1);CustomToast ct = new CustomToast(this,toast);ct.show();
?

热点排行