How to prevent Multiple Toast Overlaps

后端 未结 8 1105
难免孤独
难免孤独 2020-11-29 07:26

I\'ve been using a common \"myToast\" which I use \"myToast.cancel() prior to issuing a new toast. For Android v2.3 and older, this works great. When a new toas

8条回答
  •  时光说笑
    2020-11-29 08:01

    create new function and call this.

    ImageButton ABtn = (ImageButton) findViewById(R.id.Btn);
    ABtn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v)
    {       
    SETToast("mytext");
    }
    });
    
        private Toast toast = null;
    
    public void SETToast( String text)
    {
      if(toast==null)
      {
         toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT); 
         toast.show();
         final Handler handler = new Handler(); 
         handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                toast=null;
            }
         }, 2000);
      }
      else
      {
          toast.setText(text);
      }   
    }
    

提交回复
热议问题