温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

定制Toast的显示时间

发布时间:2020-07-31 21:22:11 来源:网络 阅读:368 作者:huangmeicai 栏目:移动开发

背景:

    缺省状态下,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的显示时间

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI