Set Toast Appear Length

前端 未结 7 2087
梦毁少年i
梦毁少年i 2020-12-02 07:47

Is there anyway I can tell a Toast Notification to show up only for a specified amount of time. Generally shorter then a regular toast message.

7条回答
  •  伪装坚强ぢ
    2020-12-02 08:34

    The stock Android Toast class is coded to accept only a Toast.LENGTH_SHORT or Toast.LENGTH_LONG parameter when calling a Toast. The values of these parameters are 0 and 1 respectively and do not accept any millisecond value when calling setDuration(); If you must show a Toast for a different duration than you may consider using a class from my SuperToasts library. The SuperToast class in the library is a mimic of the stock Android Toast class and can have any millisecond value used as a duration parameter. I do not recommend using this class to show a Toast longer than the maximum stock Android Toast length due to the lingering effect of these Toasts. I recommend that you use the SuperActivityToast class to show Toast messages in an Activity/Fragment because the Toast will be destroyed along with your Activity eliminating any chance of a lingering message. To use this class you may create a new object:

    SuperActivityToast superActivityToast = new SuperActivityToast(this);  
    superActivityToast.setDuration(SuperToast.DURATION_SHORT); 
    // setDuration(); can also accept millisecond values
    // superActivityToast.setDuration(1000);  
    superActivityToast.setText("Hello world!");  
    superActivityToast.show();  
    

    Or use the static method:

    SuperActivityToast.createDarkSuperActivityToast(this, "Hello world!", SuperToast.DURATION_SHORT).show();  
    

    There are tons of customization options you can use with the library as well, check out the Wiki page!

提交回复
热议问题