Make the button inactive and stop the toast.



  • The programme should be issued on each application ImageButton two Toast and lose the sound. In practice, if it's an accident to throw it in. ImageButton multiple times in a row, a series Toast(all rods in line), once.

    public void okv(View view) throws InterruptedException {
        view.setClickable(false);
      Random rand = new Random();
        n = rand.nextInt(5);
        mp.start(); //Это медиаплеер запускается
    
    if(n == 0)
    {
    Toast toast = makeText(getApplicationContext(),
            R.string.a1, Toast.LENGTH_LONG);
    
        toast.show();
        Toast toast1 = makeText(getApplicationContext(),
                R.string.b1, Toast.LENGTH_LONG);
        toast1.show();
        view.setClickable(true);
    

    }
    if(n == 1)
    {
    Toast toast = makeText(getApplicationContext(),
    R.string.a2, Toast.LENGTH_LONG);

        toast.show();
        Toast toast1 = makeText(getApplicationContext(),
                R.string.b2, Toast.LENGTH_LONG);
        toast1.show();
        view.setClickable(true);
    
    }
    if(n == 2)
    {
        Toast toast = makeText(getApplicationContext(),
                R.string.a3, Toast.LENGTH_LONG);
    
        toast.show();
        Toast toast1 = makeText(getApplicationContext(),
                R.string.b3, Toast.LENGTH_LONG);
        toast1.show();
        view.setClickable(true);
    
    }
    

    So the question is how to make the button really inactive? Why?

    view.setClickable(false);

    Does it work so weird?

    You need to press her a lot of times, the system only registers the first compression, shows two toasts, and only then the button becomes active again.



  • And what if there's another change, like lastTime, and a time interval more often than not, the toast code would work. And make sure the user pushes the button too often. In the code, it could look like,

    long lastTime = 0;
    long timeInterval = 1;
    

    public void okv(View view) throws InterruptedException {

    if (System.currentTimeMillis() - lastTime < timeInterval){
        return;
    }
    lastTime = System.currentTimeMillis();
    
    view.setClickable(false);
    Random rand = new Random();
    n = rand.nextInt(5);
    mp.start(); //Это медиаплеер запускается
    
    
    if (n == 0) {
        Toast toast = makeText(getApplicationContext(),
                R.string.a1, Toast.LENGTH_LONG);
    
        toast.show();
        Toast toast1 = makeText(getApplicationContext(),
                R.string.b1, Toast.LENGTH_LONG);
        toast1.show();
        view.setClickable(true);
    
    }
    if (n == 1) {
        Toast toast = makeText(getApplicationContext(),
                R.string.a2, Toast.LENGTH_LONG);
    
        toast.show();
        Toast toast1 = makeText(getApplicationContext(),
                R.string.b2, Toast.LENGTH_LONG);
        toast1.show();
        view.setClickable(true);
    
    }
    if (n == 2) {
        Toast toast = makeText(getApplicationContext(),
                R.string.a3, Toast.LENGTH_LONG);
    
        toast.show();
        Toast toast1 = makeText(getApplicationContext(),
                R.string.b3, Toast.LENGTH_LONG);
        toast1.show();
        view.setClickable(true);
    
    }
    

    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2