[Android]可自定义显示时长的Toast
2010-10-20 16:17
776 查看
package lab.sodino.customtoast; import lab.sodino.util.SodinoOut; import android.content.Context; import android.os.Handler; import android.widget.Toast; /**设计原理:在Toast显示消失之前,再次调用Toast.show()进行接力。*/ public class CustomToast { /** 用于测试 */ private int showCount = 1; private Toast toast = null; private Context context; private Handler handler = null; private Runnable toastThread = new Runnable() { public void run() { // 递增的count明显地表明是不断运行新的Toast.show()的结果。 toast.setText(String.valueOf(showCount++) + "CustomToast"); toast.show(); // 3.3秒后再度重启,设为4s的话将会看到Toast是断断续续地显示着的。 handler.postDelayed(toastThread, 3300); } }; public CustomToast(Context context) { this.context = context; handler = new Handler(this.context.getMainLooper()); toast = Toast.makeText(this.context, "", Toast.LENGTH_LONG); } public void setText(String text) { toast.setText(text); } public void showToast(final long length) { handler.post(toastThread); SodinoOut.out("Handler post at:" + System.currentTimeMillis()); Thread timeThread = new Thread() { public void run() { SodinoOut.out("TimeThread start at:" + System.currentTimeMillis()); try { Thread.sleep(length); } catch (InterruptedException e) { e.printStackTrace(); } CustomToast.this.stopToast(); SodinoOut .out("Toast canceled at:" + System.currentTimeMillis()); } }; timeThread.start(); } public void stopToast() { // 删除Handler队列中的仍处理等待的消息元素删除 handler.removeCallbacks(toastThread); // 撤掉仍在显示的Toast toast.cancel(); } }
相关文章推荐
- android135 360 来电去电归属地显示,自定义toast,
- Android 自定义Toast显示图片
- Android 自定义Toast,修改Toast样式和显示时长
- Android - Toast自定义显示时间,以及时长
- Android 自定义 Toast 显示时间
- Android进阶篇-Toast自定义显示时间
- android 自定义Toast显示风格
- Android自定义Toast,指定Toast信息显示的位置并使用Toast显示其他View
- [Android]自定义Toast显示
- android 自定义Toast,可设定显示时间
- Android实现自定义Toast,并且防止重复显示
- android 自定义Toast,可设定显示时间
- android 自定义Toast显示风格顶部弹出自定义时间
- Android 自定义Toast显示(不限时+在其他应用之上显示)
- android中Toast显示时间自定义使用
- Android 弹无虚发之第五弹:来点儿不一样的Toast(自定义Toast,设置Toast显示位置,自定义Toast的复杂布局)
- android Toast 自定义显示位置 custom position
- android自定义Toast的显示时间与消失
- android Toast工具类,可自定义位置和取消显示
- Android中Toast显示时间的自定义控制