码迷,mamicode.com
首页 > 其他好文 > 详细

定制Toast的显示时间

时间:2015-04-18 14:45:33      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:toast   android   

背景:

    缺省状态下,Toast显示时间大约在1~2秒时间,有时需要让弹出窗显示更长的时间。

案例:

    可通过调用CountDownTimeer例来达到此目标。

public class ToastActivity extends Activity
{
    AlertDialog dialog;
   
     static CountDownTimer timer =null;
     Toast toast;
     @Override
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                

                // creating toast and setting properties

                toast = new Toast(this);
                TextView textView=new TextView(this);
                textView.setTextColor(Color.BLUE);
                textView.setBackgroundColor(Color.TRANSPARENT);
                textView.setTextSize(20);
                textView.setText("This Toast will Display for 20 Seconds in Center of The Screen");
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);


                toast.setView(textView);
               

               //    Toast Display tTime Settings

               
                // Create the CountDownTimer object and implement the 2 methods
                // show the toast in onTick() method  and cancel the toast in onFinish() method
                // it will show the toast for 20 seconds (20000 milliseconds 1st argument) with interval of 1 second(2nd argument)

 
                timer =new CountDownTimer(20000, 1000)
                {
                    public void onTick(long millisUntilFinished)
                    {
                        toast.show();
                    }
                    public void onFinish()
                    {
                        toast.cancel();
                    }

                }.start();
               
          }
}

通过调用timer.cancel()可以取消Toast的显示。

结果:

    技术分享

定制Toast的显示时间

标签:toast   android   

原文地址:http://laocai.blog.51cto.com/10020033/1634769

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!