Set Toast Appear Length

前端 未结 7 2088
梦毁少年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:42

    I found a solution to this by calling toast.cancel() after a certain delay that is shorter than the standard toast duration.

            final Toast toast = Toast.makeText(ctx, "This message will disappear in 1 second", Toast.LENGTH_SHORT);
            toast.show();
    
            Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                   @Override
                   public void run() {
                       toast.cancel(); 
                   }
            }, 1000);
    

提交回复
热议问题