Can I cancel previous Toast when I want to show an other Toast?

后端 未结 11 1400
粉色の甜心
粉色の甜心 2020-12-05 17:18

In my app, I construct a calendar widget for my activity, when I scroll it to previous or next month, I let it make a toast and show it.

The question is, the toast n

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 17:55

    public static Toast  sToast=null;
    
    // create Toast object;
    
    public void showToast(String msg)
    
        {
    
        //here is checking whether toast object is null or not;if not null gonna cancel the toast that is showing in phone window and make it null;  
    
        if(sToast!=null)
        {
    
          sToast.cancel;
    
        sToast=null;
        }
    
        //if toast object is null,gonna create new instance and make it shown on phone window.
    
        if(sToast==null)
        {
    
            sToast=Toast.makeText(currentActivity.this,msg,Duration);
    
            sToast.setGravity();
    
            sToast.show();
    
        }
    
    }
    

提交回复
热议问题