How can I show a toast for a specific duration?

前端 未结 12 2160
自闭症患者
自闭症患者 2020-12-08 01:08

This is the way I have to show the Toast for 500 milliseconds. Though, it\'s showing more than a second.

Toast.makeText(LiveChat.this, \"Typing\         


        
相关标签:
12条回答
  • 2020-12-08 01:53

    I tried different method and this method works for me

     final Toast mytoast = Toast.makeText(getApplicationContext(), jsonObject.getString("response_message"), Toast.LENGTH_SHORT);
     mytoast.show();
    
                            Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    mytoast.cancel();
                                }
                            }, 5000);// 5 sec
    
    0 讨论(0)
  • 2020-12-08 01:53

    I have created a class ToastMessage in droid side.

       public class ToastMessage: IToast
            {
                public void LongAlert(string message)
                {
                    Toast toast = Toast.MakeText(Android.App.Application.Context, message, ToastLength.Short);
                    toast.Show();
                    Device.StartTimer(TimeSpan.FromSeconds(0.5), () =>
                    {               
                       toast.Cancel();
                        return false;
                    });
                }
            }
    

    I have created interface IToast

     public  interface IToast
        {
            void LongAlert(string message);
        }
    

    Calling By Dependency Service

     DependencyService.Get<IToast>().LongAlert("Right Answer");
    
    0 讨论(0)
  • 2020-12-08 01:54

    Can't do what you are asking with the standard Toasts. Perhaps you should think about integrating a 3rd party library that gives you better Toast options (named Crouton). I haven't used it myself, but people seem to like it.

    You can't control the length of Toasts in the standard OS.

    Crouton link: https://github.com/keyboardsurfer/Crouton

    0 讨论(0)
  • 2020-12-08 01:55
    Toast.makeText(LiveChar.this,"Typing",Toast.LENGTH_SHORT);
    

    This is the only way you can..

    0 讨论(0)
  • 2020-12-08 01:56

    This cannot be done. To show a toast for a length shorter than Toast.LENGTH_SHORT, you must cancel it after the time you want. Something like:

    final Toast toast = Toast.makeText(getApplicationContext(), "This message will disappear in half a second", Toast.LENGTH_SHORT);
        toast.show();
    
        Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
               @Override
               public void run() {
                   toast.cancel(); 
               }
        }, 500);
    
    0 讨论(0)
  • 2020-12-08 01:56

    I don't believe this can be done, you can only use Toast.LENGTH_LONG or Toast.LENTH_SHORT you can't define your know speed.

    0 讨论(0)
提交回复
热议问题